Reputation: 1875
Can sed print text between PATTERN1 and PATTERN2 OR PATTERN3? For instance, for both samples below I want the output shown:
Sample 1
Not lemur
Lemur
Ring-Tailed
Mouse
Catta
Fusa
Bad
Sample 2
Not lemur
Lemur
Ring-Tailed
Mouse
Catta
Desired output:
Lemur
Ring-Tailed
Mouse
Catta
My sed command so far is sed -n '/^Lemur/,$p'
, but that also prints the "Fusa\n Bad", which I don't want. Does sed have an "OR" feature, like '/^Lemur/,($|^[^\s])p'
with the intent to match from Lemur to the end of the input or a line that does not start with whitespace. Also, I want to only print the lines after the first pattern (Lemur) that start with whitespace (i.e. I do not want to print "Fusa" in the first sample).
Upvotes: 0
Views: 420
Reputation: 58361
This might work for you (GNU sed):
sed ':a;/^Lemur/{:b;n;/^\S/ba;bb};d' file
Label the beginning of the the cycle.
If a line begins with the word Lemur
:
All other lines delete.
An alternative:
sed -n '/^\S/{:a;x;/^Lemur/p;$q;x;h};//!H;$ba' file
Upvotes: 1
Reputation: 203169
sed is the best tool for doing s/old/new/
on individual strings. For anything more complicated than that, just use awk.
$ awk '/^\S/{f=/^Lemur/} f' file1
Lemur
Ring-Tailed
Mouse
Catta
$ awk '/^\S/{f=/^Lemur/} f' file2
Lemur
Ring-Tailed
Mouse
Catta
If you don't have GNU awk for \S
then just use [^[:space:]]
instead.
Upvotes: 1