LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-24-2006, 01:38 PM   #1
aevangelica
LQ Newbie
 
Registered: Oct 2005
Distribution: openSUSE 10.2
Posts: 20

Rep: Reputation: 0
Quick sed question


Ioha,

I was just wondering if sed can write directly back to the file it reads from. As it stands, if i want to change foo to bar i do:

sed "s|foo|bar|g" $file > temp$file
mv temp$file $file


but that just seems wasteful to me. Thoughts?
 
Old 05-24-2006, 01:39 PM   #2
dubz_444
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Arch Linux
Posts: 43

Rep: Reputation: 15
your way works...

this doesnt

cat bob.pa | sed 's/greg/sammy/g' > bob.pa

U can use your way for editing files that you want to keep the same name

Last edited by dubz_444; 05-24-2006 at 01:43 PM.
 
Old 05-24-2006, 10:55 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by dubz_444
this doesnt

cat bob.pa | sed 's/greg/sammy/g' > bob.pa
Actually, the above works just fine---i just tried it.
 
Old 05-25-2006, 02:47 AM   #4
dubz_444
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Arch Linux
Posts: 43

Rep: Reputation: 15
well, my file comes out empty!
 
Old 05-25-2006, 03:02 AM   #5
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 31
Quote:
Originally Posted by aevangelica
Ioha,

I was just wondering if sed can write directly back to the file it reads from. As it stands, if i want to change foo to bar i do:

sed "s|foo|bar|g" $file > temp$file
mv temp$file $file


but that just seems wasteful to me. Thoughts?
GNU sed version 4.0.9:

sed --help [drastically snipped]
Quote:
-i[suffix], --in-place[=suffix]
edit files in place (makes backup if extension supplied)
so
sed -ibak "s|foo|bar|g" $file outputs the modified file and creates file.bak(the original). No extension, no backup.
 
Old 05-25-2006, 10:55 AM   #6
aevangelica
LQ Newbie
 
Registered: Oct 2005
Distribution: openSUSE 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
Excellent, thanks vls!
 
  


Reply

Tags
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
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
quick sed quesion DBabo Linux - Software 1 07-24-2005 01:11 PM
Quick sed question scuffell Linux - General 1 02-11-2005 05:02 PM
Quick question: quoting text in sed overbored Linux - Software 0 06-24-2004 01:23 PM
sed question dolvmin Linux - Software 4 03-09-2004 09:27 AM


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

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