Add a line to a specific spot in a file

Hello,

Simple if you know how, which I don't.

I would like to add a line to file file.env

file.env consists of the following :

line 1-20 here ..
# Begin customizations

ADDED_LINE_TO_GO_HERE

# End customizations
eof

I would like to use a shell script to add the line ADDED_LINE_TO_GO_HERE, but it has to come after # Begin customizations

Any idea how to do this please ?

Cheers,
Dave

sed "/# Begin customizations/s//&\\
ADDED_LINE_TO_GO_HERE/" file > tmp
mv tmp file

thanks anbu23

how would I deal with something that had $ and / in it please ?
eg
instead of ADDED_LINE_TO_GO_HERE
it was
$ADDED_LINE/to/go/here

I tried the following, but it didn't work ..

sed "/# Begin customizations/s//&\\
$ADDED_LINE/to/go/here/" file > tmp
mv tmp file

it said, sed command was garbled.

Thanks for your time and help, much appreciated.

sed "/# Begin customizations/s//&\\
\$ADDED_LINE\/to\/go\/here/" file > tmp

Hi Anbu23 and thanks again

so do I have to put a \ in front of $ . ; = , etc ?? I presume this means ignore the fact that it is there and carry on

I have been trying what is below and I get the garbled message again ..

sed "/# Begin customizations/s//&\\
X_TOP=\$A_TOP\/xx\/11.5.0;export X_TOP" a.env > a.tmp

I also tried this and got the same message

sed "/# Begin customizations/s//&\\
X_TOP\=\$A_TOP\/xx\/11\.5\.0\;export X_TOP" a.env > a.tmp

What am I doing wrong please ?

Thanks again.
Dave

sed "/# Begin customizations/s//&\\
X_TOP=\$A_TOP\/xx\/11.5.0;export X_TOP/" a.env > a.tmp

You missed the forward slash

You have to add \ before special characters like $ / etc

Thanks ! :slight_smile: