Choose a line-number to add line

Hello guys,

I'm making a script to add visudo with this script.

Do you guys know if it's possible to add words to a line-number you want to.

Something like this:

echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)?

Thanks!

linenumber=16
sed "${linenumber}s/$/sometext/" file

will append sometext to line 16 of file.

EDIT: If you want to edit the file inplace then check if your sed supports -i . If not then you'll need to redirect to a temp file and move/copy the files around.

1 Like

You can do it with sed. i.e.

sed "16i\ 
newline goes here
" /etc/sudoers

EDIT: -i (if the option is available) will overwrite the original file. Always take a backup first!
EDIT 2: LOL CarloM and me are editing in sync :smiley:

2 Likes

Thanks scott,

I also found a other option:

sed -i "3i HELLO" /etc/sudoers

EDIT: And thanks CarloM