Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

insert a line with file which has sed command in linux

I am trying to insert line in a file which will have a sed command to insert a line in another file
Like below for eg :
want to add this line sed -i '1s/^/insideFile\n/' secondFile.sh to 65th line of firstfile.txt

I tried:

sed -i '65s/^/\'sed -i \'1s/^/insideFile\n/\' secondFile.sh\'\n/' firstfile.sh

but not able to escape the '

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

also tried

sed -i "65s/^/'sed -i "1s/^/topLine\n/" $FILE_HOME_LOCAL_FILE'\n/" secondFile.sh

but got
sed: -e expression #1, char 18: unknown option to `s

>Solution :

You may use this sed:

sed -i.bak "65s~^~sed -i '1s/^/insideFile\\\\n/' secondFile.sh\\n~" firstfile.sh

Note:

  • Use of alternate delimiter ~ to avoid escaping /
  • use of double quotes for your sed command to avoid escaping single quotes
  • use of \\\\ to insert a single \
  • use of \\n inside double quotes to insert a line break

Alternatively with i (insert) instead of s (substitute):

sed -i.bak "65i\\
sed -i '1s/^/insideFile\\\\n/' secondFile.sh\\
" firstfile.sh
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading