LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-24-2010, 05:46 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
sed question - open, write same file


I'd like to open, edit, write the same file using sed --all in a single line. If I do this:

sed 's/foo/bar/' /tmp/myfile

the contents of the file are displayed with the modification. I've tried
variations:

sed 's/foo/bar/' </tmp/myfile> /tmp/myfile
cat /tmp/myfile | sed 's/foo/bar/' >/tmp/myfile

but I typically end up with an empty file.

I read about the x, hold buffer --but it didn't fully make sense to me.

Thanks a bunch for the help.
 
Old 09-24-2010, 05:47 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Take your first example, and add the -i switch (for inplace edit)

Code:
sed -i 's/foo/bar/' /tmp/myfile
 
1 members found this post helpful.
Old 09-24-2010, 05:51 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
::smacks forehead: ugh! I totally overlooked that one! Thank you!
 
Old 09-24-2010, 05:54 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
No worries - sed & friends have a lot of options; easy to overlook one or more of what you're looking for!
 
Old 09-25-2010, 12:38 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Please mark as SOLVED once you have a solution.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 03:24 AM
Sed wizard wanted to write oneliner shy_guest Linux - Newbie 7 09-15-2009 10:25 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 05:23 AM
Getting PIDs which have files open / waiting for file write to complete prell Programming 2 03-29-2005 10:39 AM
A question about to write file. richardH Programming 1 01-05-2004 07:19 AM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration