Sed -- deleting to line BEFORE regexp match

Leo de Wit leo at philmds.UUCP
Tue Feb 21 06:18:34 AEST 1989


In article <3296 at uhccux.uhcc.hawaii.edu> lupton at uhccux.uhcc.hawaii.edu (Robert Lupton) writes:
|I have a patch generator (a script using diff -c, of course), and I want
|to exclude the patches for certain files. So I want to delete all lines
|from 
|
|diff -c -r old_file new_file
|...
|diff -c -r OLD_FILE NEW_FILE
|
|(but keeping the second diff line), using sed. 
  [some lines removed]

If the second address expression would uniquely identify the last line of
a range of lines, it is easy:

/firstexpr/,/lastexpr/{
    /lastexpr/!{
    etc.
    }
}

However, in this case the first line matches the second address
expression, so we have to do something else. The following will only
need one extra line for each file you want to 'exclude' in the patch:

sed "
/^diff/{
    : again
    /new_file1$/b del
    /new_file2$/b del

       (etc.)

    /new_fileN$/b del
    b nodel
    : del
    n
    /^diff/b again
    b del
    : nodel
}"

|Also, I couldn't make this work using csh (it complains about unmatched "),
|only with the Bourne shell.

Csh doesn't like newlines in quoted strings; you'll have to quote them
(using a backslash character).

      Leo.



More information about the Comp.unix.questions mailing list