Parsing a diff file using grep/awk

awkdiff()

I'm trying to parse a standard diff of some sql files to return only the delete sections. I have been using grep with the after context (-A) which almost works (only because I know that delete sections will all be very short). e.g.

diff $$_$1.sql $$_$2.sql|egrep -A3 "[01234567889][01234567889]d[01234567889][0123456789]"

I am thinking that with AWK, I could tell it start at (the above regex) and stop at the first line starting with a digit or the first line ending with a —

I have played around a bit, but can't seem to find the right syntax to do this. Can this be done with AWK? or is there another tool I should use?

Best Answer

I am thinking that with AWK, I could tell it start at (the above regex) and stop at the first line starting with a digit or the first line ending with a --

Please give us an example if it is not what you want:

sed -n '/[0-9][0-9]d[0-9][0-9]/,/^[0-9]\|--$/p'

EDIT

Although you've accepted my answer but I still want to edit my post to share with you a regex that can help you solve your problem thoroughly. sed allows you excluding the matching lines with b - branch command:

sed -n '/[0-9][0-9]d[0-9][0-9]/,/^[0-9]\|--$/ { /^[0-9]/b; p }'

but with this regex, sed also remove the REGEX1. So, Lookahead appears in my mind:

sed -n '/[0-9][0-9]d[0-9][0-9]/,/^[0-9]\|--$/ { /^[0-9](?:(?![0-9]d[0-9][0-9]).*)$/b; p }'

but it not works because the sed, awk, grep uses the POSIX RE flavor which doesn't support negative lookahead. You should try with Python, Perl, Ruby, ...