Skip to content
Advertisement

Printing all the lines that contain a certain word exactly k times

I have to search for all the lines from a file which contain a given word exactly k times. I think that I should use grep/sed/awk but I don’t know how. My idea was to check every line by line using sed and grep like this:

JavaScript

My first problem is that I get the following error : syntax error near unexpected token 'sed'. Then I realize that for my test file the command sed -n -'p1' test.txt | grep -w -c "ab" doesn’t return the exact number of apparitions of “ab” in the first line from my file (it returns 1 but there are 3 apparitions). My test.txt file:

JavaScript

Advertisement

Answer

awk to the rescue!

JavaScript

note that < and > word boundaries might be gawk specific.

for variable assignment, I think easiest will be

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement