For example my text is like this below.
mykey
nice comment
...
...
mykey
not bad comment
...
...
mykey
nice comment
...
...
mykey
excellent comment
I am using grep such as
$ grep 'mykey' * -1 | wc -l
$ 4
$ grep 'mykey' * -1 | grep 'nice comment' | wc -l
$ 2
This can check the number of mykey
appears and nice comment
is succeeded.
Then I want to see where the nice comment
is not appeared after mykey.
At first I try this ,but it was wrong.
grep 'mykey' * -1 | grep -v 'nice comment'
I want to show such as,
mykey
not bad comment
mykey
excellent comment
Is it possible?
CodePudding user response:
I don't think it is possible with grep
, but you can use sed
:
sed -n '/mykey/{N;/nice comment/!p;}' master.txt
Output:
mykey
not bad comment
mykey
excellent comment
If you only want to count number of "not nice" comments, you could still use two greps:
$ grep -A1 --group-separator= 'mykey' master.txt | grep -v 'nice comment\|mykey\|^$' | wc -l
2