Re: Sed replace multiple lines
From: Michael Tosch (eedmit_at_NO.eed.SPAM.ericsson.PLS.se)
Date: 03/14/05
- Next message: Michael Tosch: "Re: Checking for Disk Space in Current Path"
- Previous message: Dan Mercer: "Re: How to pipe or give a parameter list of unknown size?"
- In reply to: OptiAce_at_ePepper.org: "Sed replace multiple lines"
- Next in thread: Ed Morton: "Re: Sed replace multiple lines"
- Reply: Ed Morton: "Re: Sed replace multiple lines"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 14 Mar 2005 21:01:25 +0100
OptiAce@ePepper.org wrote:
> I am trying to do a wilcard, multi-line replace in sed. I'm basically
> designating a start and end point and trying to replace those points
> and everything between them. I've used a ~ as the delimiter in sed
> since there are slashes in the find and replace strings. Here is my
> completely broken attempt at this:
>
> sed -e "s~^<Directory />.*\$<\/Directory>~
> <Directory \/>
> Options FollowSymLinks
> AllowOverride None
> AuthType Basic
> AuthName \"You must login for access to this area:\"
> AuthUserFile /etc/passwd.apache
> AuthGroupFile /etc/group.apache
> Require valid-user
> </Directory>" <httpd.conf >httpd.conf.new
>
> Will any amount of tweaking make this work, or am I on totally the
> wrong track? Thanks!
This doesnt work because a regular expression cannot span over multiple
lines. Well, in sed it can: when you paste two lines into the sed buffer via
"N", you can specify \n to match an embedded newline:
sed "N;s~^<Directory />.*\n</Directory>~<Directory />
...
But in this case you certainly want to replace additional text in between.
The following does a "d" command on the entire text block,
and for one specific line (here: first line) it prints the new text.
sed '/^<Directory \/>/,/^<\/Directory>/{
/^<Directory \/>/{
i\
<Directory />\
Options FollowSymLinks\
AllowOverride None\
AuthType Basic\
AuthName "You must login for access to this area:"\
AuthUserFile /etc/passwd.apache\
AuthGroupFile /etc/group.apache\
Require valid-user\
</Directory>
}
d
}' <httpd.conf >httpd.conf.new
Note that the preceding lines of the new text need to end with
a "\" , otherwise sed treats the next line as a new command.
-- Michael Tosch @ hp : com
- Next message: Michael Tosch: "Re: Checking for Disk Space in Current Path"
- Previous message: Dan Mercer: "Re: How to pipe or give a parameter list of unknown size?"
- In reply to: OptiAce_at_ePepper.org: "Sed replace multiple lines"
- Next in thread: Ed Morton: "Re: Sed replace multiple lines"
- Reply: Ed Morton: "Re: Sed replace multiple lines"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|