[comp.unix.questions] Sed -- deleting to line BEFORE regexp match

lupton@uhccux.uhcc.hawaii.edu (Robert Lupton) (02/20/89)

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. I can do it in awk rather
clumsily, but as I'm already sed'ing the diff output I'd rather just
add another -e. The following works as part of the pipeline, but surely
there's a better way! You are only allowed to use the string `new_file' 
only once, so I'll be able to add other files that I want to ignore.

sed -e "/^diff new_file/,/^diff/ {
	:l1
	/\ndiff/bl2
	/^diff/N
	/^diff/bl1
	:l2
	s/^.*\ndiff/diff/
}" 

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

Any comments? What I want is "/^diff new_file/,(/^diff/-1)d", but that
ain't sed.

			Robert
				(lupton@uhccux.uhcc.hawaii.edu)

maart@cs.vu.nl (Maarten Litmaath) (02/21/89)

lupton@uhccux.uhcc.hawaii.edu (Robert Lupton) writes:
\...
\Also, I couldn't make this work using csh (it complains about unmatched "),
\only with the Bourne shell.

Welcome to csh! Its quoting conventions are a BIT different, RTFM.

\Any comments? What I want is "/^diff new_file/,(/^diff/-1)d", but that
\ain't sed.

sed -n '/^diff new_file/{
		:L
		n
		/^diff/!b L
	}
	p'
-- 
 "Those who do not understand Henry     |Maarten Litmaath @ VU Amsterdam:
Spencer are condemned to reinvent DOS." |maart@cs.vu.nl, mcvax!botter!maart

leo@philmds.UUCP (Leo de Wit) (02/21/89)

In article <3296@uhccux.uhcc.hawaii.edu> lupton@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.

jerbil@cit-vax.Caltech.Edu (Stainless Steel Gerbil [Joe Beckenbach]) (02/24/89)

In his article Robert Lupton (lupton@uhccux.uhcc.hawaii.edu) 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. 

	Can you spare an extra pass on it? If so, just duplicate the
last line and then delete from the first to the second inclusive.

<inews fodder>
<inews fodder>
<inews fodder>
<inews fodder>
<inews fodder>
<inews fodder>
<inews fodder>
-- 
Users I'd like to see:	[-:		| Joe Beckenbach
	Postmaster@link.L5.edu		| jerbil@cit-vax.caltech.edu
	gorby@cparty.kremlin.gov.cccp	| Caltech 256-80, Pasadena CA 91125