Adding a lines to specific section of the file.

Hello,

I have to a add 2 lines to /etc/sudoers file under this section below, can someone please suggest script to add these two lines when execute this remotely on to a multiple servers.

before
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

After
## Allow root to run any commands anywhere
root## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
man    ALL=(ALL)       ALL
moon    ALL=(ALL)       ALL

Thanks,

Use the "a" subcommand of "sed" to append a line. You need to escape every embedded newline in the appended string by a "\". Use some regexp to anchor at a certain line. For instance: append line after a line starting with "@@":

sed '/^@@/ a \
some text\
some more text' /path/to/infile > /path/to/outfile

before:

before...
@@
after...

afterwards:

before...
@@
some text
some more text
after...

I hope this helps.

bakunin

If you just want to add lines to end of a file then use append command

$echo 'Line Insert Test 1
quote> Line 2 Test' >> filename # take a backup of original file if required 

If its to insert after a pattern then,

$sed '/^root/s/.*/& \
quote> Test line 1\
quote> Line 2 to test/' infile > outfile