[comp.unix.questions] The solution to the sed - multiple lines problem

bill@hao.UUCP (03/12/87)

This is the solution of the "sed - matching multiple lines on input" problem
which I posed a few days ago.  I thought I'd post it to the net to let people
know I've solved it and just how to do it.
Given the following input:

one
two
three

and the following sed editor script:

s/one\ntwo\nthree/one, two, three/g

Why don't it work?  Well here's my hypothesis:
	Sed reads the first line which contains the string "one\n" and 
discards the newline.  So the pattern space looks like "one".  Of course the
pattern "one\ntwo\nthree" won't match that.  So the second line gets read 
and the same thing happens, then the third line and so nothing ever matches.
The solution was to build up the pattern space by using the "hold buffer".
The following sed script does the job:

/one/!b
N
/two/!b
N
/three/!b
s/\n/, /g 

See the sed writeup to understand the commands !, b and N.  Thanks to all who
responded with positive suggestions.

--Bill

chris@mimsy.UUCP (03/12/87)

In article <574@hao.UCAR.EDU> bill@hao.UCAR.EDU (Bill Roberts) writes:
>The following sed script does the job:

>/one/!b
>N
>/two/!b
>N
>/three/!b
>s/\n/, /g 

Alas, this script fails to convert the input

	one
	one
	two
	three
	four

to

	one
	one, two, three
	four

While I think it is possible to handle this case, and its counterpart
`one\ntwo\none\ntwo\nthree', I am not going to work on it.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu