Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to insert some text in a file

Tags:

linux

sed

I'm trying to use SED to make some changes to a CUPS configuration file.

What I want to do is find the following text:

# Restrict access to the server...
<Location />
  Order allow, deny

and append this to it:

  Allow from all

I've tried sed '/# Restrict access to the server...\n<Location />\n Order allow, deny\n/ a\ Allow from all' etc/cups/cupsd.conf but I don't know how to escape the >, and prepending a backslash to it does not work.

like image 403
Jake Petroules Avatar asked May 25 '26 12:05

Jake Petroules


1 Answers

This will work for you:

sed 'N;/<Location \/>\n *Order allow, deny/s//&\n  Allow from all/;P;D' file

or alternatively

sed $'N;/<Location \\/>\\n *Order allow, deny/a\\\nAllow from all\nP;D;' file

Example

$ sed 'N;/<Location \/>\n *Order allow, deny/s//&\n  Allow from all/;P;D' cups.in
# other stuff here
# Restrict access to the server...
<Location />
  Order allow, deny
  Allow from all
</Location>
stuff

$ sed $'N;/<Location \\/>\\n *Order allow, deny/a\\\n  Allow from all\nP;D;' cups.in
# other stuff here
# Restrict access to the server...
<Location />
  Allow from all
  Order allow, deny
</Location>
stuff

Note that the order of where the Allow from all is added is different, although I don't believe this makes a bit of difference functionality wise.

like image 158
SiegeX Avatar answered May 28 '26 00:05

SiegeX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!