ammu
1
Hi,
I'm looking for one liner code for counting number of words in a delimited string..
I know about wc -w ..but if i give wc -w a.txt,b.txt it won't work with delimited sting as it was looking for files with a.txt and b.txt
I know there will be a simple solution..i couldn't resolve..myself
Ex:In a string a.txt,b.txt,abacd i need the output as 3..
Please help!
methyl
2
On (of many) ways: Change the delimiter to a newline
echo "a.txt,b.txt,abacd"|tr ',' '\n'|wc -l
3
$
$
$ echo "a.txt,b.txt,abacd" | perl -lne 's/[^,]//g && print length($_)+1'
3
$
$ echo "a.txt,b.txt,abacd" | perl -lne '$_.=","; print s/,//g'
3
$
$ echo "a.txt,b.txt,abacd" | awk '{$0=$0","; print gsub(",",x)}'
3
$
$ # and Perl equivalent of Franklin52's awk script ;)
$ echo "a.txt,b.txt,abacd" | perl -F, -plane '$_=$#F+1'
3
$
$
tyler_durden
Or:
awk -F, '{print NF}' file
ammu
5
Thank You Guys! It works like a charm!
To get the count of the no of occurrences of the given delimiter (i.e) comma ',' here
awk -F, '{print NF-1}' file