-   Linux - General (
-   -   How do you use sed to edit a file? (

SparceMatrix 09-04-2010 01:38 PM

How do you use sed to edit a file?
I need to change one word in over a thousand pages of HTML. I think sed is the only way to do this, but I've never used sed before.

After reading the documentation, I see that it is pretty easy to alter files to standard output, in other words, the screen.

But I don't want to do just alter text to the screen, I want to alter the original file, else I am not doing any Stream EDiting as near as I can tell.

None of the documentation explains how to take a file, apply sed to alter that file. How do you do this?

I'm pretty clear on the basics,


[myuser@myhost dir]#sed s/changethis/tothis/ /mydir/myfile
How do I apply sed to actually make the changes to myfile?

AlucardZero 09-04-2010 01:46 PM


sed s/changethis/tothis/ /mydir/myfile > /tmp/newfile
mv /tmp/newfile /mydir/myfile

Or use sed's --in-place option, from the man page:

      -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

SparceMatrix 09-04-2010 02:50 PM

Thanks AlucardZero, -i did the trick. Perl from the command line has a similar feature. My "Linux in a Nutshell" is a ten year old edition. Time to upgrade.

Why would I want to use the redirection and mv command? Did you imagine I might have a really old Linux distribution?

AlucardZero 09-04-2010 04:13 PM

It's just another way to do things.

All times are GMT -5. The time now is 11:10 PM.