Oscar Sayin Oscar Sayin - 9 months ago 75
Bash Question

Insert a text before slash "/" character in bash scripts

I have a line in a file as below:

2 14 * * * /run/opt/server/autoi.sh


And I want to insert "root" before /run/opt/server/autoi.sh as below:

2 14 * * * root /run/opt/server/autoi.sh


I have tried the following command

sed '//run/i root' filename


but it gives the following error:

sed: -e expression #1, char 0: no previous regular expression


Could you please help me to find a fix for it?

Answer Source

Use a different separator character, and you need to use the s command to substitute, e.g:

sed 's@/run@root /run@' filename

or without repeating /run:

sed 's@/run@root &@' filename
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download