I would like to count the number of time '_' (under score) is appearing in my file name. How do I do that?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
echo $filename | tr -c -d _ | wc -c
回答2:
I'd use tr.
$ echo "8979858774_/hkjhjkh_kjh.hjghjg/_jhkj/_/" | tr -d _ -c | wc -c
4
回答3:
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:
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*