[comp.unix.questions] i command in sed - only one address?

ceblair@ux1.cso.uiuc.edu (Charles Blair) (04/29/91)

   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.''
Something like this seems to work when I insert a line before every
line with an A in it (by using /A/ instead of /A/,/B/ )

Charles Blair

jik@athena.mit.edu (Jonathan I. Kamens) (04/30/91)

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

dattier@vpnet.chi.il.us (David W. Tamkin) (05/01/91)

In article <1991Apr29.131718.26624@ux1.cso.uiuc.edu>
ceblair@ux1.cso.uiuc.edu (Charles Blair) wrote:

| 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.''

Believe it or not, this has worked in my experience:

sed '
/A/,/B/{
i\
INSERTED LINE
}' < file1 > file2

Since the braces protect the 'i' command from seeing the range and thus 'i'
is being fed only one address at a time, it doesn't complain.

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier@vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591

"Parker Lewis Can't Lose" mailing list:
flamingo-request@esd.sgi.com (relay)  flamingo-request@ddsw1.mcs.com (digest)

gwc@root.co.uk (Geoff Clare) (05/01/91)

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.''

jik@athena.mit.edu (Jonathan I. Kamens) writes:

>  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
>}

There's a much simpler way:

sed '/A/,/B/{
i\
INSERTED LINE
}' < file1 > file2

-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273