PDA

View Full Version : sed to replace text


Greslore
February 12th, 2007, 05:26 PM
Short, sweet, and quick question:

contents of /etc/blah.conf:
blah1
blah1
blah2
blah2
blah1

How do I use sed to change all the blah1 in /etc/blah.conf with the word scoobie?

LotsOfPhil
February 12th, 2007, 05:29 PM
sed -i 's/blah1/scoobie/g' /etc/blah.conf

The -i makes it edit the file in place.

Greslore
February 12th, 2007, 05:35 PM
Bingo.. that did it. I was using:

sed 's/blah1/scoobie/g' /etc/blah.conf

That -i did the trick. Thank you kindly good sir =)

jblebrun
February 12th, 2007, 05:43 PM
sed -i 's/blah1/scoobie/g' /etc/blah.conf

The -i makes it edit the file. It's the same as sed '...' /etc/blah.conf > /etc/blah.conf

CAUTION! No, it's not the same!!

The second command will obliterate your blah.conf file, turning it into an empty file. I've been bittern by that one before.

LotsOfPhil
February 12th, 2007, 07:30 PM
True, true. I meant in a pseudocode kinda way. I edited the post to remove the offending (damaging :)) passage.