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:

line=1
while [ (sed -n -'($line)p' $name) -n ]; do
    if [ (sed -n -'($line)p' $name | grep -w -c $word) -eq "$number" ]; then
        sed -n -'($line)p' $name
    fi
    let line+=1
done

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:

abc ab cds ab abcd edfs ab
kkmd ab jnabc bad ab
abcdefghijklmnop ab cdab ab ab
abcde bad abc cdef a b

Advertisement

Answer

awk to the rescue!

$ awk -F'\<ab\>' -v count=2 'NF==count+1' file   

kkmd ab jnabc bad ab

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

for variable assignment, I think easiest will be

$ word=ab; awk -F"\<$word\>" -v count=2 'NF==count+1' file 

kkmd ab jnabc bad ab
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement