priyam
1
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
Thanks,
Priya
echo "$mystring" | awk -F'-' '{ print NF }'
priyam
3
but this is showing output as 6 instead of 5 ?Why is that so ?
joeyg
4
> 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 }'
priyam
7
The above methods are working ... Thanks:)
joeyg
8
> echo "SYS_NAME-123-S5-2008-10-20.LOG" | awk 'BEGIN {FS="-"}{print NF-1}'
5
danmero
9
For each NF(number of fields) we have (NF-1) delimiters.