LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-04-2010, 12:38 PM   #1
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 216

Rep: Reputation: 30
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?
 
Old 09-04-2010, 12:46 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,621

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
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)
 
1 members found this post helpful.
Old 09-04-2010, 01:50 PM   #3
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 216

Original Poster
Rep: Reputation: 30
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?
 
Old 09-04-2010, 03:13 PM   #4
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,621

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
It's just another way to do things.
 
  


Reply

Tags
command, editing, line, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to edit html tkeyser Programming 6 08-20-2009 07:15 PM
SED: edit ip address n1wil Linux - Software 4 01-08-2008 02:18 PM
help to edit Edit my bootloader's config file from kernel prompt jagdishpandey Linux - Kernel 5 07-03-2007 06:59 AM
can sed edit individual characters/portions of a sentence? TGWDNGHN Linux - Software 10 10-18-2005 04:50 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM


All times are GMT -5. The time now is 09:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration