IN-Decent

Re-decentralizing internet with free software

Insert Text Before or After a Particular Line With sed

Posted at — Jul 13, 2020

You have a text file and you want to append multiple lines after a line containing particular string, a command can be used as below

sed -i".bkp_`date +%Y%m%d`" '/\[Login\]/a RemoveIPC=no' /etc/systemd/logind.conf

Above command adds a line below the line [Login] in /etc/systemd/logind.conf file.

Similarly to add a line before the pattern i command can be used.

sed -i.bkp '/\[Login\]/i #Disabled removing of user IPC \
#DO NOT COMMENT RemoveIPC line' /etc/systemd/logind.conf

NOTES:

References:

  1. https://unix.stackexchange.com/questions/121161/how-to-insert-text-after-a-certain-string-in-a-file