I need to count the lines containing the words
an in a text file (
poem.txt), but not those containing both.
I've tried using
grep -c the poem.txt | grep -c an poem.txt
but this gives me the wrong answer of 6 when the total number of
an is 9 lines.
I do want to count the lines containing the words and not the words themselves. Only the actual word should count, so
the but not
an but not
Where is the misty shark?
Where is she?
The small reef roughly fights the mast.
Where is the small gull?
Where is he?
The gull grows like a clear pirate.
Clouds fall like old mainlands.
She will Rise calmly like a dead pirate.
Eat an orange.
Warm, sunny sharks quietly pull a cold, old breeze.
All ships command rough, rainy sails.
Elvis Aaron Presley also known simply as the Elvis
He is also referred to as the King
The best-selling solo music artist of all time
He was the most commercially successful artist in many genres
He has many awards including a Grammy lifetime achievement
Elvis in the 1970s has numerous jumpsuits including an eagle one.
To clarify further: how many lines in the poem contain
an but you should not count the lines that include both
the car is red - this counted
an apple is in the corner - not counted
hello i am big - not counted
where is an apple - counted
So here the output should be 2.
Edit: I'm not worried about case sensitivity.
Final edit: Thanks for all your help.
i've managed to solve the problem. i used the one of the answer and changed it a little. i used
cat poem.txt | grep -Evi -e '\<an .* the\>' -e '\<the .* an\>' | grep -Eci -e '\<(an|the)\> how ever i did change the
-c in the second grep to a
-n for some added info.
Yet again thank you for all the help!! :)