Take the following file :
$ cat f1
stu vwx yza
uvw xyz abc
abc def ghi
def ghi jkl
ghi jkl mno
jkl mno pqr
mno pqr stu
pqr stu vwx
stu vwx yza
To print all lines from the first one containing abc
to the first one containing mno
with GNU sed
:
$ sed -n '/abc/,/mno/p' f1
uvw xyz abc
abc def ghi
def ghi jkl
ghi jkl mno
How could I print all lines until the last one containing mno
, e.g. how could I get the following result :
uvw xyz abc
abc def ghi
def ghi jkl
ghi jkl mno
jkl mno pqr
mno pqr stu
In other words, is there a way to make GNU sed
's range selection greedy ?
Update
In my setting :
- If
mno
is missing, it should print out everything until the end of the file. mno
cannot occur before the firstabc
.- There's always at least one
abc
, andabc
andmno
are never on the same line
EDIT
I just added a dummy stu vwx yza
line at the start, so that the file doesn't start with a line including abc
(to avoid solutions that start from the first line - they should start from the first line having abc
in it)
abc
, andabc
andmno
are never on the same line. Onlymno
can be missing. I'm not looking for a solution that can handle all theoretic cases. So far I have 3 awk, 2 sed and 1 perl solutions that are all perfectly working.