LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do you use sed to edit a file? (http://www.linuxquestions.org/questions/linux-general-1/how-do-you-use-sed-to-edit-a-file-830339/)

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,

Code:

[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

Code:

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

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

      -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 09:35 AM.