Lets take the below content as an example
This file is a test file
this file is used to count the word 'file' in this test file
there are multiple occurrences of word file in some lines in this test file
I want to count the word 'file' in the above content.
I'm using the below shell command
cat $filename | sed "s/_/new/g" | sed "s/$word/_/g" | tr -c -d _ |wc -c
Is that ok or any better ideas ..?
Using tr for separating words and then grep and wc seems possible :
tr -s ' ' '\n' < file.txt | grep file | wc -l
grep $word $filename -o | wc -l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With