Good morning,
I am trying to write a UNIX script for a file with multiple duplicate patterns, followed by the pattern I am looking for. This question is very similar to:
"Get the last occurrence of a pattern before another pattern". However I do not have 'tac' (or 'tail -r'), and am looking to return the last occurrence of String1 before String2, and String2 also.
output.out:
...
Condition 1: A
foo
Condition 1: B
foo
Condition 2: C
foo
Condition 1: D
foo
Condition 1: E
foo
Condition 2: F
...
I'd like to write a grep / sed / awk script to return:
Condition 1: B
Condition 2: C
Condition 1: E
Condition 2: F
Since Condition 1 is always 'x' lines above Condition 2, I have tried using:
grep -B x "Condition 2"
And going from there, but for some reason the second occurrence in the resulting script output stops a few lines from the top (therefore not displaying Condition 1), due to blank lines in between Condition 1 / 2 maybe, I'm not sure the reason. So I've abandoned using grep -B. I think tac would help as I could grep 'backwards', but I don't have the tac command (or tail -r) on my environment.
Was wondering if anyone had any advice, would be much appreciated. Thanks for your time and help in advance.
tac
orsort -r
? – DopeGhoti Dec 20 '16 at 21:00sed '/Condition 1/h;/Condition 2/!d;x;/Condition 1/!d;G' file
... so instead ofq
uitting like there just append hold space to pattern spaceG
and there you have it... – don_crissti Dec 21 '16 at 13:42