[ale] Sed help with append

Michael H. Warfield mhw at WittsEnd.com
Tue Mar 23 12:31:09 EDT 2010


On Tue, 2010-03-23 at 11:34 -0400, Jim Seymour wrote: 
> Hi,

> I am trying to expand my capabilities with sed use. What I am trying
> to do is modify an apache virt file as follows. The file containing
> the lines to modify is named test.

> <VirtualHost 12.34.567.89>
> ServerAdmin webmaster at domain.com
> ServerName domain.com
> DocumentRoot /does/not/matter
> </VirtualHost>

> It needs a ServerAlias appended after ServerName as www.domain.com

> What am I missing to get the pattern match in the append?

> jim at swift ~ $ sed '/ServerName \(.*\)/ a\ServerAlias www.\1' test
> <VirtualHost 12.34.567.89>
> ServerAdmin webmaster at domain.com
> ServerName domain.com
> ServerAlias www.1
> DocumentRoot /does/not/matter
> </VirtualHost>

I don't see any way you would have a prayer of making that work.
There's two things wrong with it.  The first and foremost is that append
and insert are not part of a regex.  They are literal text strings.  So
you can pretty much forget about that path.  Second problem is that, I
don't believe you can use a regex remembered selection from an address
range pattern even in a substitute pattern, which would be a regex.  It
has to be remembered from the first part of the regex and used in the
second part for the substitute.

Here...  Try this.  It's a bit more complex and obtuse, but this should
do what you want to do:

sed -e '/ServerName /{' -e h -e 's/ServerName \(.*\)/ServerAlias www.\1/' -e x -e G -e '}'

That only operates on the lines with ServerName and copies it to hold
space (h).  Then modifies the copy in pattern (s) space and then swaps
pattern and hold space (x) and appends (G) the modified copy after the
unmodified copy.  Then you terminate the action group and it prints.

> Have not been able to locate anything by googling about combining an
> append with a remembered pattern. I even tried combining it into an
> append the line and then do a substitute on the appended line to no
> avail. I have a feeling there is probably something simple I have
> missed somewhere.
> 
> Thanks,

Mike
-- 
Michael H. Warfield (AI4NB) | (770) 985-6132 |  mhw at WittsEnd.com
   /\/\|=mhw=|\/\/          | (678) 463-0932 |  http://www.wittsend.com/mhw/
   NIC whois: MHW9          | An optimist believes we live in the best of all
 PGP Key: 0x674627FF        | possible worlds.  A pessimist is sure of it!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 482 bytes
Desc: This is a digitally signed message part
Url : http://mail.ale.org/pipermail/ale/attachments/20100323/3495cf0a/attachment.bin 


More information about the Ale mailing list