I want to count the occurences of a word in a file with only one line of code. Example: for the input: "hi, welcome to the himalaya. hihi" "hi" the output will be 4 (because the word "hi" occurs 4 times)
I tried using tr and grep, but it doesn't count "hihi" twice, but only once.
tr 'WORD' '\n' < $1 | grep $2 | wc -l
For the given example above, the output was 3 instead of 4. Thank you!