The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
redirection DNAx86 Shell Programming and Scripting 9 04-18-2008 11:24 AM
csh stderr redirection jolok Shell Programming and Scripting 0 04-15-2005 07:03 AM
I/O redirection within a coprocess Mugin Shell Programming and Scripting 4 10-21-2003 05:17 AM
Help with redirection Shallon1 High Level Programming 2 12-12-2001 04:35 AM
redirection to tty** with cat zorro UNIX for Dummies Questions & Answers 1 11-02-2001 08:23 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-11-2008
Registered User
 

Join Date: Mar 2008
Location: Greece
Posts: 14
sed redirection

I am messing around with sed and I observed the following.

I have as test file a file named errors which contains:

Quote:
> This is the first line of an example text.
> It is a text with erors.
> Lots of erors.
> So much erors, all these erors are making me sick.
> This is a line not containing any errors.
> This is the last line.
I want to remove the > from the beginning of the lines. So I use the following command.

Code:
sed 's/^> //' errors
which apparently works. Then, I want to redirect the results back to the original file. So I tried this:

Code:
sed 's/^> //' errors > errors
but now the file errors contains nothing. Why?

When I redirect the output to another file it seems to work. For example the file asdf contains the results:

Code:
sed 's/^> //' errors > asdf
Furthermore, when I try:
Code:
echo `sed 's/^> //' errors` > errors
it seems to work but the characters of new line are replaced by spaces. I guess this is because of the usage of echo. But what is different now and the redirection works?
Is it because the output is guaranteed that will be redirected after it is completely calculated?
What's the difference?

I am looking forward to your replies.

Last edited by myle; 03-11-2008 at 04:56 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 03-12-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 555
Hi.

This is a succinct description of how the shell can destroy the file in your situation:
Quote:
You can combine input redirection with output redirection, but be careful not to use the same filename in both places. For example:

$ cat < output > output

will destroy the contents of the file output. This is because the first thing the shell does when it sees the > operator is to create an empty file ready for the output.

-- Introduction to UNIX and Linux: Lecture 4
The problem is not with sed, it will be with any command because it's the result of the shell.

It's common to run across this apparently surprising outcome in early use of *nix -- best wishes ... cheers, drl
Reply With Quote
  #3  
Old 03-12-2008
Registered User
 

Join Date: Mar 2008
Location: Greece
Posts: 14
Quote:
Originally Posted by myle View Post
Code:
echo `sed 's/^> //' errors` > errors
So, I guess this worked because the
Code:
sed 's/^> //' errors
was executed before anything else in the line and the result substitutes the contents of `...` before proceeding to the rest of the code?
So, it's a matter of priorities, right?
Reply With Quote
  #4  
Old 03-12-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 555
HI.
Quote:
Originally Posted by myle View Post
... So, it's a matter of priorities, right?
If we consider priorities as the predefined sequence of events that the shell goes through to process a command, yes.

I usually refer people to the O'Reilly book on the bash shell or the Korn shell. In the former, around page 178 is a flow-chart and explanation of the steps.

Step 7 is command substitution ( the back-quotes or the more modern $(...) ), and step 12 is the setup of I/O redirection, etc. ... cheers, drl
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0