How do I count the number of occurance of '_&#

2019-07-20 00:53发布

I would like to count the number of time '_' (under score) is appearing in my file name. How do I do that?

标签: shell unix
4条回答
我只想做你的唯一
2楼-- · 2019-07-20 01:24

Probably not the most elegant or perfect solution, but should do the trick:

echo $filename|split -C 1 - /tmp/foobar
grep -l '_' /tmp/foobar* |wc -l
rm /tmp/foobar*
查看更多
萌系小妹纸
3楼-- · 2019-07-20 01:25

Another variation:

echo "$filename" | grep -o _ | wc -l

Or for shells that support this, such as Bash, ksh and zsh:

u=${filename//_}
echo $((${#filename} - ${#u}))
查看更多
做自己的国王
4楼-- · 2019-07-20 01:32
echo $filename | tr -c -d _ | wc -c
查看更多
淡お忘
5楼-- · 2019-07-20 01:49

I'd use tr.

$ echo "8979858774_/hkjhjkh_kjh.hjghjg/_jhkj/_/" | tr -d _ -c | wc -c
   4
查看更多
登录 后发表回答