How do I count the number of dots in a string in BASH? For example
VAR=\"s454-da4_sd.fs_84-df.f-sds.a_as_d.a-565sd.dasd\"
# Variable VAR contains 5 dots
You can do it combining grep
and wc
commands:
echo "string.with.dots." | grep -o "\." | wc -l
Explanation:
grep -o # will return only matching symbols line/by/line
wc -l # will count number of lines produced by grep
Or you can use only grep
for that purpose:
echo "string.with.dots." | grep -o "\." | grep -c "\."