I want to add some text on a line:
sudo sed -i '5imytext 16/16' /file
Now I've added mytext 16/16
on line 5 of the file, but I actually want to add the text 'mytext' 16/16
(mytext
between single quotes).
I tried
sudo sed -i '5i'mytext' 16/16' /file
but it didn't work. Can someone help me?
The single quotes that you're trying to use in your insertion string are interfering with the ones around the sed command.
The simplest thing to do is to use different quotes around your sed command:
"5i'mytext' 16/16"
Normally it's best to use single quotes around a sed command but it would be more tricky in this case:
'5i'"'"'mytext'"'"' 16/16'
Basically, you need to put the single quotes inside double quotes somehow and in this case there's no reason not to double quote the whole command.
As suggested by 123 in the comments, an alternative would be to put your sed command into a script file:
5i'mytext' 16/16
Then use the -f
switch to sed:
sed -f script
This avoids the need to use two kinds of quotes.
Use double quote in these cases. Because:
'\''
won't work)"'\""
will work)Example:
sudo sed -i "5i'mytext' 16/16" /file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With