PDA

View Full Version : sed noob - text insertion question


schotty
11th October 2010, 09:19 AM
Greetings,

I have a need to insert text into a file prior to a string.

Basically I wish to append some commands to rc.local, but PRIOR to the exit 0 command. However EVERY tutorial out there for some reason is missing something that apparently must be understood by everyone sans me. How to get the output to modify the file rather than just echo it to the terminal. I got the sed command sorta right, actually since I want to add a comment, two sed commands:

sed /'exit 0'/i'TEST' < blah.sh

And blah.sh is just a dummy file so I can make sure this works right. All thats in there is the exit 0 line.

Any tips as to what I omitted or glossed over?
Thanks,
Andrew.

smr54
11th October 2010, 01:48 PM
i is for insert.

A few minutes of googling sed append line to end of file came up with this, which works.

sed '$a\exit 0' blah.sh

http://www.linuxquestions.org/questions/linux-general-1/bash-programming-append-single-line-to-end-of-file-265534/

Was where I found it, though you'll note I give a simplified version. cat blah|sed can almost always be replaced by sed '/whatever/' blah

Gödel
11th October 2010, 05:16 PM
you need to use '-i' option for 'in place' editing

ie

sed -i '/exit 0/iTEST' blah.sh

forkbomb
11th October 2010, 05:26 PM
Alternatively, do a good ole' bash redirect to leave the original in place and give yourself a new file. (That way you can keep the old rc.local). I'd do


sed /'exit 0'/i'TEST' rc.local > rc.local.new


(I'm not seeing the point of reading the input file blah.sh via shell redirection < when sed's standard syntax takes an infile as an argument. sed [OPTION]... [input-file])

schotty
15th October 2010, 06:17 PM
Thanks guys, all of the answers worked great.

I know a bit of bash, but zero on sed and awk. I think its about time I educate myself. These are quite powerful tools, should I ever get around to truly learning them.

TimGJ
18th October 2010, 10:21 AM
I know a bit of bash, but zero on sed and awk. I think its about time I educate myself. These are quite powerful tools, should I ever get around to truly learning them.

At the risk of starting a religious war, you might also like to consider learning something like perl instead. I believe it can do pretty much all that sed and awk can and then some more. My first "real" programming job was on an old Unix platform in the late 1980s and I therefore grew up using tools like awk and sed. When I learned perl many years later it really opened my eyes.

HTH.

Tim.

droidhacker
21st October 2010, 05:41 PM
At the risk of starting a religious war, you might also like to consider learning something like perl instead. I believe it can do pretty much all that sed and awk can and then some more. My first "real" programming job was on an old Unix platform in the late 1980s and I therefore grew up using tools like awk and sed. When I learned perl many years later it really opened my eyes.

HTH.

Tim.

That's right up there with using a nuclear bomb to hunt ducks.

Bossangoa Instagram Photos - Soeke Photos on Instagram - Radomyshl Instagram Photos