Reputation: 1029
I have a text file and I would like to count for each line the number of appearances of a given word for example if the word is "text" and the file is
abc text fff text text jjj
fff fff text ddd
eee rrr ttt yyy
I expect the output
3
1
0
How can I achieve this with bash?
Upvotes: 0
Views: 182
Reputation: 22438
while read line
do
echo $(grep -o "text" <<< "$line" | wc -l)
done < file
Upvotes: 0
Reputation: 60143
while read line; do echo "$line" |tr ' ' '\n' |grep text -c ; done < file
Upvotes: 1