The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 02:24 PM
csh stderr redirection jolok Shell Programming and Scripting 0 04-15-2005 10:03 AM
I/O redirection within a coprocess Mugin Shell Programming and Scripting 4 10-21-2003 08:17 AM
Help with redirection Shallon1 High Level Programming 2 12-12-2001 07:35 AM
redirection to tty** with cat zorro UNIX for Dummies Questions & Answers 1 11-02-2001 11:23 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2008
myle myle is offline
Registered User
  
 

Join Date: Mar 2008
Location: Greece
Posts: 16
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 07:56 PM..
  #2 (permalink)  
Old 03-12-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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
  #3 (permalink)  
Old 03-12-2008
myle myle is offline
Registered User
  
 

Join Date: Mar 2008
Location: Greece
Posts: 16
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?
  #4 (permalink)  
Old 03-12-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0