Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude one string from bash output

Tags:

linux

bash

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.

like image 826
user3016814 Avatar asked Mar 11 '23 13:03

user3016814


1 Answers

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 }'
like image 70
janos Avatar answered Mar 16 '23 00:03

janos