i want to find the no:of occurrences of a word in a file
cat 1.txt
unix script unix script
unix script unix script unix script unix script
unix script unix script unix
unix
script
unix script unix script
now i want to find , how many times 'unix' was occurred
please help me
thanks in advance.
Try this
awk '{for(i=1;i<=NF;i++)a[$i]++}END{print a[cnt]}' cnt=unix input_file
--ahamed
joeyg
3
tr " " "/n" <1.txt | grep "unix" | wc -l
Puts all words onto own lines, changing space to carriage-return
greps for the string
give a line count for matches
Yoda
4
If you have GNU grep:
grep -o "unix" file | wc -l
---------- Post updated at 13:50 ---------- Previous update was at 13:43 ----------
@joeyg, I noticed a typo, also wc -l
can be replaced with grep -c
switch:
tr " " "\n" < file | grep -c "unix"
1 Like
In case it is undesirable, note that all of the solutions offered will match subword strings.
Regards,
Alister
joeyg
6
tr " " "\n" <1.txt | grep "^unix$" | wc -l