Embedded newlines in sed

lem at alice.UUCP lem at alice.UUCP
Fri Mar 13 06:50:35 AEST 1987


To match patterns spread over more than one line, you have to
get more than one line of the file into the pattern space.
the 'N' command reads the next line of the file into the pattern space,
appending it to whatever is already there, and separating it from what is there
by a '\n' character (012).

Patterns in the script are matched against whatever is in the pattern-space.

The following script should find consecutive groups of lines:

	one
	two
	three

and turn them into:

	one, two, three

just copying everything else in the input file.

	$q
	N
	$q
	/^.*\n.*\n/!N

	/^one\ntwo\nthree$/!{
		P
		D
	}

	s/\n/, /g

If the above script is in the file sedscript,
and the input file:

	one
	two
	one
	two
	three
	four
	five
(without the leading tabs) is in input,
the command
	sed -f sedscript input
shoud produce

	one
	two
	one, two, three
	four
	five

as output (again, without leading tabs).

It works with the 8th (9th) Edition sed that I support;
subtle changes were introduced at UCB and in System V
(different in the two cases);
mail me if you have trouble.

Lee McMahon   ---   ihnp4!research!lem



More information about the Comp.unix.questions mailing list