Issue
Team,
I need to insert a string at a certain line in a file after certain tabs.
bonus: I also need to skip insertion if it already exists.
cat test.txt
1
2
3
4
5
sed -i '3i\ \t\t\t\t<file branch-rate="0.0">' test.txt
cat test.txt < after above insertion.
1
2
<file branch-rate="0.0">
3
4
5
using variable assignment
ln=5
sed -i '${ln}i\ \t\t\t\t<file branch-rate="0.0">' test.txt
sed: -e expression #1, char 4: extra characters after command
Solution
You need to use double quotes "
so your variables can expand. It is also a good idea to create a backup file while using in-place editing.
You can try this sed
$ sed -i.bak "${ln}i\ \t\t\t\t<file branch-rate=\"0.0\">" test.txt
Answered By - sseLtaH Answer Checked By - Marilyn (WPSolving Volunteer)