Newsgroups: comp.unix.questions
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-picayune.mit.edu!athena.mit.edu!jik
From: jik@athena.mit.edu (Jonathan I. Kamens)
Subject: Re: i command in sed - only one address?
Message-ID: <1991Apr29.201315.7790@athena.mit.edu>
Sender: news@athena.mit.edu (News system)
Organization: Massachusetts Institute of Technology
References:  <1991Apr29.131718.26624@ux1.cso.uiuc.edu>
Date: Mon, 29 Apr 91 20:13:15 GMT
Lines: 37

In article <1991Apr29.131718.26624@ux1.cso.uiuc.edu>, ceblair@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@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710
