Insert a line at specific line number with sed or awk

Viewed 255349

I have a script file which I need to modify with another script to insert a text at the 8th line.

String to insert: Project_Name=sowstest, into a file called start.

I tried to use awk and sed, but my command is getting garbled.

11 Answers
sed -i "" -e $'4 a\\\n''Project_Name=sowstest' start
  • This line works fine in macOS

it is working fine in linux to add in 2 lines.

sed '2s/$/ myalias/' file

Thank you umläute

sed -i "" -e $'4 a\\\n''Project_Name=sowstest' filename

the following was usefull on macOS to be able to add a new line after the 4

In order to loop i created an array of folders, ti iterate on them in mac zsh

for foldercc in $foldernames; 

sed -i "" -e $'4 a\\\n''Project_Name=sowstest' $foldercc/filenames;
Related