Thursday, October 15, 2009

sed edit files in place

Suppose you want to change 'ENABLED="false"' to true in one of the various /etc/default configuration files in debian. You would probably use sed if you wanted to make this change in lots of places. By default sed just dumps output to STDOUT. To directly edit a file with sed, you either have to edit a file, save the changes elsewhere, and move the changes back to the original file. Or, you can use -i with sed for 'edit files in place.'

Old and busted:
sed 's/2006/2007/g' oldfile > tmpfile ; mv tmpfile oldfile

New hotness:
sed -i 's/2006/2007/g' file

Additional reading for sed:

No comments:

Post a Comment