hi I hace a string
"abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
i replaced commas with spaces, now i want to calculate nof occurences of "abc" word.
thanks in advance
Satya
hi I hace a string
"abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
i replaced commas with spaces, now i want to calculate nof occurences of "abc" word.
thanks in advance
Satya
If you will replace comma(,) with newline then you can use awk to get number of occurrence of 'abc' like this :-
cat testfile | tr "," "\n" | awk /abc/ | wc -l
sed 's/,/\n/g' testfile | awk /abc/ |wc -l
try this........
awk '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' abc
in case, ur file is a comma delimited one, then.....
awk -F, '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' abc
if you have Python
#!/usr/bin/env python
s="abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
print s.count("abc")
or at the shell
# s="abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
# echo $s | python -c "print raw_input().count('abc')"
5
simply try this
awk 'BEGIN{RS=","}{if($0=="abc"){x += 1}}END{print x}' filename
hi vijay,
what u gave is good, but where i need to pass the string or file name
awk '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' <filename>
Actually, I 'd named the i/p file as "abc"..so the confusion..
sorry for that