开发者

How do I count the number of occurance of '_' in my file name using Unix shell script

I would like to count the number of time '_' (under score) is appearing in my 开发者_如何学编程file name. How do I do that?


echo $filename | tr -c -d _ | wc -c


I'd use tr.

$ echo "8979858774_/hkjhjkh_kjh.hjghjg/_jhkj/_/" | tr -d _ -c | wc -c
   4


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}))


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*
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜