Delete range of lines above pattern with sed (or awk)
Sed doesn't backtrack: once it's processed a line, it's done. So “find a line and print the previous N lines” isn't going to work as is, unlike “find a line and print the next N lines” which is easy to graft on.
If the file isn't too long, since you seem to be ok with GNU extensions, you can use tac
to reverse the lines of the file.
tac | sed '/banana/I,+2 d' | tac
Another angle of attack is to maintain a sliding window in a tool like awk. Adapting from Is there any alternative to grep's -A -B -C switches (to print few lines before and after )? (warning: minimally tested):
#!/bin/sh
{ "exec" "awk" "-f" "$0" "$@"; } # -*-awk-*-
# The array h contains the history of lines that are eligible for being "before" lines.
# The variable skip contains the number of lines to skip.
skip { --skip }
match($0, pattern) { skip = before + after }
NR > before && !skip { print NR h[NR-before] }
{ delete h[NR-before]; h[NR] = $0 }
END { if (!skip) {for (i=NR-before+1; i<=NR; i++) print h[i]} }
Usage: /path/to/script -v pattern='banana' -v before=2
This is pretty easy with ex or vim -e
vim -e - $file <<@@@
g/banana/.-2,.d
wq
@@@
The expression reads: for every line containing banana in the range from the current line -2 to the current line, delete.
What's cool is that the range can also contain backwards and forwards searches, for example this will delete all sections of the file starting with a line containing apple and ending with a line containing orange and containing a line with banana:
vim -e - $file <<@@@
g/banana/?apple?,/orange/d
wq
@@@
Also note that up to ten vim/ex commands can be submitted using the inline command option "-c". See the man page.
vim -e -c 'g/banana/.-2,.d' -c 'wq' $yourfilename
and
ex -c 'g/banana/?apple?,/orange/d' -c 'wq' $yourfilename
You can do this fairly simply with sed
:
printf %s\\n 1 2 3 4match 5match 6 \
7match 8 9 10 11match |
sed -e'1N;$!N;/\n.*match/!P;D'
I don't know why anyone would say otherwise, but to find a line and print previous lines sed
incorporates the built-in P
rint primitive which writes only up to the first \n
ewline character in pattern space. The complementary D
elete primitive removes that same segment of pattern space before recursively recycling the script with what remains. And to round it off, there is a primitive for appending the N
ext input line to pattern space following an inserted \n
ewline character.
So that one line of sed
should be all you need. You just replace match
with whatever your regexp is and you're golden. That should be a very fast solution as well.
Note also that it will correctly count a match
immediately preceding another match
as both a trigger to quiet output for the previous two lines and quiet its print as well:
1
7match
8
11match
In order for it to work for an arbitrary number of lines, all you need to do is get a lead.
So:
printf %s\\n 1 2 3 4 5 6 7match \
8match 9match 10match \
11match 12 13 14 15 16 \
17 18 19 20match |
sed -e:b -e'$!{N;2,5bb' -e\} -e'/\n.*match/!P;D'
1
11match
12
13
14
20match
...deletes the 5 lines preceding any match.