user3016814 user3016814 - 4 months ago 14
Linux Question

Exclude one string from bash output

I'm working now on a project. In this project for some reasons I need to exclude first string from the output (or file) that matches the pattern. The difficulty is in that I need to exclude just one string, just first string from the stream.
For example, if I have:

1 abc
2 qwerty
3 open
4 abc
5 talk


After some script working I should have this:

2 qwerty
3 open
4 abc
5 talk


NOTE: I don't know anything about digits before words, so I can't filter the output using knowledge about them.

I've written small script with grep, but it cuts out every string, that matches the pattern:

'some program' | grep -v "abc"


Read info about awk, sed, etc. but didn't understand if I can solve my problem.
Anything helps, Thank you.

Answer

Using awk:

some program | awk '{ if (/abc/ && !seen) { seen = 1 } else print }'

Alternatively, using only filters:

some program | awk '!/abc/ || seen { print } /abc/ && !seen { seen = 1 }'