I need to count the lines containing the words the
and 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 the
and 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 there
, and an
but not Pan
.
Example file: poem.txt
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 the
or an
but you should not count the lines that include both the
and an
.
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!! :)
pan
as an occurrence ofan
or not. Also clarify if you want to count the lines or the number of times each word appears.grep -c the poem.txt | grep -c an poem.txt
does not what you expect. It first counts the number of lines with the. The output, i.e. the number, is piped into a second grep, which ignores its input. It counts the lines containing an and outputs that. Besides, the twogrep
commands also count lines that contain there or demand.The
&the
on line 3) How about case sensitivity?best-selling
one word or 2 (or even 3 - best, selling, and best-selling)? As always with these sorts of questions the definition of a "word" is key.