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/^/insideFilen/' secondFile.sh to 65th line of firstfile.txt
I tried:
sed -i '65s/^/'sed -i '1s/^/insideFilen/' secondFile.sh'n/' firstfile.sh
but not able to escape the '
also tried
sed -i "65s/^/'sed -i "1s/^/topLinen/" $FILE_HOME_LOCAL_FILE'n/" secondFile.sh
but got sed: -e expression #1, char 18: unknown option to `s
Advertisement
Answer
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
sedcommand to avoid escaping single quotes - use of
\\to insert a single - use of
\ninside 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
Cleanest solution would be to create a sed script like this thus avoiding all quoting and extra escaping:
cat ins.sed 65i sed -i '1s/^/insideFile\n/' secondFile.sh
Then use it as:
sed -i.bak -f ins.sed firstfile.sh