Delimiter count in a string.

Hi ,

I need to get the delimiter "-" count in a particular string.

string= SYS_NAME-123-S5-2008-10-20.LOG

the delimit "-" count is 5 .

Using sed or awk can I know the count ?

I have seen how to get the count for delimiter in a file but not a string :frowning:

Thanks,
Priya

echo "$mystring" | awk -F'-' '{ print NF }'

but this is showing output as 6 instead of 5 ?Why is that so ?

> echo `echo "SYS_NAME-123-S5-2008-10-20.LOG" | tr "-" "\n" | wc -l` -1 | bc
5

Or:

string=SYS_NAME-123-S5-2008-10-20.LOG ifs="$IFS" IFS=-
set -- $string; count=$(($#-1));IFS="$ifs"

The variable count contains the number of -'s.

With zsh:

count=$((${#${(s.-.)string}}-1))

echo "SYS_NAME-123-S5-2008-10-20.LOG"|awk -F'-' '{ print NF-1 }'

The above methods are working ... Thanks:)

> echo "SYS_NAME-123-S5-2008-10-20.LOG" | awk 'BEGIN {FS="-"}{print NF-1}'
5

For each NF(number of fields) we have (NF-1) delimiters.