Re: Sed replace multiple lines

From: Michael Tosch (eedmit_at_NO.eed.SPAM.ericsson.PLS.se)
Date: 03/14/05


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


Relevant Pages

  • Re: Sed replace multiple lines
    ... >> designating a start and end point and trying to replace those points ... >> Options FollowSymLinks ... >> AllowOverride None ... > This doesnt work because a regular expression cannot span over multiple ...
    (comp.unix.shell)
  • Re: How To Make index.shtml As A Home Page?
    ... is the correct command, but will only be effective if AllowOverride All ... overridden (provided AllowOverride is set) using .htaccess as you have done. ... > DirectoryIndex index.shtml index.html index.htm index.php ... you should be able to open your online web in FrontPage and rename ...
    (microsoft.public.frontpage.client)
  • Re: template column checkbox problem in grid view
    ... inherited from the collectionbase on the page that doesnt work, ... command name "use" to the command handler and a grid view with the ... the checkbox is just a template i made by placeing an asp.net control ... Protected Sub ToolbarCommand(ByVal sender As Object, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Sed replace multiple lines
    ... > You can use a mixture of shell and awk to do this without having to ... > Options FollowSymLinks ... > AllowOverride None ... > Require valid-user ...
    (comp.unix.shell)
  • Can someone help me?
    ... I am using a command line visual basic ... Why is it that it doesnt work when I compile the following? ... I have confirmed that xyz etc are all valid paths! ... then I can extend it so that TEST.BAT becomes and input ...
    (microsoft.public.vb.general.discussion)