The solution to the sed - multiple lines problem

bill at hao.UUCP bill at hao.UUCP
Thu Mar 12 10:11:50 AEST 1987


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



More information about the Comp.unix.questions mailing list