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 10-24-2009, 03:21 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Very strange problem of sed


Hi all...
I am using fedora 9, bash shell

Contents of file fruits.txt
Quote:
apple
banana
mango
Code:
sed 's/banana/Orange/' fruits.txt > fruits.txt
is making the file empty....why is this so..I strongly feel it should change only banana to Orange...

can anyone please help me..
 
Old 10-24-2009, 03:46 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If you want to change the file, then use sed's -i flag.
 
Old 10-24-2009, 04:38 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by Nylex View Post
If you want to change the file, then use sed's -i flag.
Excellent Nylex....
Nylex rocks !!

It works...

But why

Code:
sed 's/banana/Orange/' fruits.txt > fruits.txt
doesn't work....Anyone please.....
 
Old 10-24-2009, 04:58 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
because you are redirecting to the file itself... choose a different filename.
 
Old 10-24-2009, 05:00 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The shell takes a look at the command first, sees that a redirect is needed (the > fruits.txt part) and sets this up by creating an empty (!!) file called fruits.txt. Then sed starts doing its thing, but the input file is now empty (it was replaced by the empty file bash created).

Since sed 4.0 the -i option was introduced (Nylex's solution). Before that you needed to use a temporary file and a copy/move command:
sed 's/x/y' foo > bar
mv bar foo

Hope this clears things up a bit.
 
Old 10-24-2009, 05:14 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Hi druuna...

Yes..I understood the concept...You explained it very clearly...

Thanks again sir...
 
Old 10-24-2009, 06:36 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by vinaytp View Post
Hi all...
I am using fedora 9, bash shell

Contents of file fruits.txt


Code:
sed 's/banana/Orange/' fruits.txt > fruits.txt
is making the file empty....why is this so..I strongly feel it should change only banana to Orange...

can anyone please help me..
Never, and I mean never, read and write a file this way. The OS opens the file for reading, but it also opens it for writing and erases its contents before you can read from it.

If you have to read, change and write one file, open a temp file like this:

Code:
$ tempfile=$(mktemp)
$ command < sourcefile.txt > $tempfile
$ mv $temnpfile sourcefile.txt
 
  


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
sed and character classes , strange results crispyleif Linux - Newbie 9 02-10-2009 04:08 AM
using sed to remove a strange token jacksmash Programming 8 09-11-2008 11:20 AM
tr, sed, and strange stdout problem d1s4st3r Programming 7 01-24-2008 11:01 PM
strange SED question pixellany Linux - Software 8 12-02-2007 06:04 AM
strange sed/bash behavior mpdavig Programming 1 07-24-2004 03:27 AM


All times are GMT -5. The time now is 08:23 AM.

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