I've spent some time searching the web, but didn't find the answer. Let's say I have a file containing the following lines :
aaaaaaa
vvvvv
ggggg
yyyyyyyyy
ffffff
rrrrrrrr
uuuuu
ssssssssssss
zzzzz
hhhhhhhh
I know how to find all lines from the one containing "ffffff" to the one containing "uuuuu" using awk :
awk '/ffffff/,/uuuuu/' file
But how can I get also the line preceding the first one I found (i.e. "yyyyyyyyy")? Is there something like grep -B 1 to do this? What I want to get is :
yyyyyyyyy
ffffff
rrrrrrrr
uuuuu
Thanks in advance.
To only print if both ffffff and uuuuu exist in the input and not print a blank line if ffffff is the first line in the input:
$ cat tst.awk
/ffffff/ { f = 1 }
f {
rec = rec $0 ORS
if ( /uuuuu/ ) {
printf "%s", rec
f = 0
}
next
}
{ rec = $0 ORS }
$ awk -f tst.awk file
yyyyyyyyy
ffffff
rrrrrrrr
uuuuu
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With