sed redirection


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed redirection
# 1  
Old 03-11-2008
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. Smilie

Last edited by myle; 03-11-2008 at 08:56 PM..
# 2  
Old 03-12-2008
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  
Old 03-12-2008
Quote:
Originally Posted by myle
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  
Old 03-12-2008
HI.
Quote:
Originally Posted by myle
... 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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

about different redirection

explain the redirections 1>, 2>, 3>, ..... and 1< ,2<,3<..... where we use these things thanks Thread moved from AIX forum (2 Replies)
Discussion started by: tsurendra
2 Replies

2. Shell Programming and Scripting

Redirection

Hello All, I am using the below script to gather various tools running by the user, we have more than 100 tools running on the server so my challenge is to redirect memory & cpu load to the file with the name of the tool.so am using the below script i am stucking how to redirect to the file... (2 Replies)
Discussion started by: ajaincv
2 Replies

3. Shell Programming and Scripting

I/O redirection

Hello everyone,I'm reading a book and there's code fragment: exec 3>&1 ls -l 2>&1 >&3 3>&- | grep bad 3>&- exec 3>&- It says that the red part of that code does not close fd 3 but the green does close the fd 3.I can't understand that.....Why?Any predicate will be appreciated.:) (18 Replies)
Discussion started by: homeboy
18 Replies

4. UNIX for Dummies Questions & Answers

Help with Redirection

Hi Guys, I m new to UNIX and new to this forum. Was wondering if someone can help me understand redirection (standard input output pipeline etc) for starters, not too sure what this would mean who | sort > sortedfile | pr | lp im starting to understand common commands but when throwing... (2 Replies)
Discussion started by: jmack123
2 Replies

5. Homework & Coursework Questions

Using Pipes and Redirection

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a pipe to show the number of people who are logged into the system right now. Create a pipe to show... (2 Replies)
Discussion started by: lakers34kb
2 Replies

6. Shell Programming and Scripting

Redirection using csh

I have a csh script which I am using to run a program set data = $argv set inmod = $argv set nxz = $argv # Remove the file extension .pmod set data = ` echo $data | awk 'BEGIN { FS=".dat" } { print $1 }' ` set inmod = ` echo $inmod | awk 'BEGIN { FS=".vmod" } { print... (8 Replies)
Discussion started by: kristinu
8 Replies

7. Shell Programming and Scripting

sed error : Syntax error: redirection unexpected

My script is throwing the error 'Syntax error: redirection unexpected' My line of code.. cat nsstatustest.html | sed s/<tr><td align="left">/<tr><td align="left" bgcolor="#000000"><font color="white">/ > ztmp.Ps23zp2s.2-Fpps3-wmmm0dss3 HTML tags are getting in the way but they're needed to... (3 Replies)
Discussion started by: phpfreak
3 Replies

8. Shell Programming and Scripting

redirection

Hi, The code below works, it's a part of a bash shell script that serve to search a pattern $pattern_da_cercare in the files contained in a directory $directory_iniziale. Now the proble is: How can I redirect stderr to a file? PS: so I want to redirect ALL the errors to a file. I tryed... (9 Replies)
Discussion started by: DNAx86
9 Replies

9. UNIX for Dummies Questions & Answers

Input Redirection

Hi everybody, first of all i am a new member in UNIX.com and this is my first post. I am impressed with the amount of information a person can ever have in this forum, it is really great having something similiar; anyways let me tell you about the problem I am having, hope you will answer me.... (6 Replies)
Discussion started by: majeed73
6 Replies

10. Programming

Help with redirection

Here is my problem. I don't know make this redirection thing work. The output file (called output.c) looks like this #include<stdio.h> int main() { int k; int m; print f("%d\n", k); printf("%d\n", m); return 0; } the input file(called input.c) is this #include<stdio.h> int... (2 Replies)
Discussion started by: Shallon1
2 Replies
Login or Register to Ask a Question