i command in sed - only one address?

Jonathan I. Kamens jik at athena.mit.edu
Tue Apr 30 06:13:15 AEST 1991


In article <1991Apr29.131718.26624 at ux1.cso.uiuc.edu>, ceblair at ux1.cso.uiuc.edu (Charles Blair) writes:
|>    I would like to put a line before ranges in a file.  Something like
|> 
|> sed '/A/,/B/i\
|> INSERTED LINE' < file1 > file2
|> 
|> but when I try this, I get an error message saying ``only one address.''

  Because, as sed is pointing out, the 'i' command only takes one address. 
You need to loop through the lines in the region, inserting the text before
each of them.  Something like this:

/A/{
	:loop
	i\
INSERTED LINE
	/B/b
	n
	b loop
}

This tells sed that starting at /A/, it should insert the line of text before
each line, then branch to the end of the script (that's what the empty 'b'
command means) if /B/ has been reached; otherwise, print the line, read the
next line (both of these are done by 'n') and go to the top of the loop.

  I never thought I'd be doing this, but....

perl -pe 'if (/A/../B/) {print "INSERTED LINE\n";}'

print "Just another perl hacker,\n"

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list