Redirect overwrites itself


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect overwrites itself
# 1  
Old 05-21-2010
Redirect overwrites itself

I bet many people faced this problem before:

The command below will result in a blank file:

Code:
$ cat myfile | grep pattern > myfile

Is there any easy way to do it?
# 2  
Old 05-21-2010
Yes. Don't do it Smilie

Write to another file, and copy the new file over the old one if you have to.
# 3  
Old 05-21-2010
Quote:
Originally Posted by scottn
Yes. Don't do it Smilie

Write to another file, and copy the new file over the old one if you have to.
I've always done that, but it seems like a waste of time.
There has to be a general solution for this problem. SmilieSmilieSmilieSmilieSmilie
# 4  
Old 05-21-2010
Code:
sed -ix -n "/pattern/p" file1

# 5  
Old 05-21-2010
Code:
sed -ie '/pattern/!d' myfile

Handle with care! Smilie
# 6  
Old 05-21-2010
Grep was just an example. Let's say I'm editing the file with tr,cut,awk,etc.
We can't rely on sed for everything.

Ps: thanks a lot for your response Scottn, but I believe there has to be a better way.
# 7  
Old 05-21-2010
The sed ways shown above is the GNU sed way - not the (for my two cent's worth) standard way.

The other tools that you mention, GNU or otherwise, most likely don't have that "flexibility".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sendmail MX redirect

Hi I have a working sendmail setup for my domain, but would like to run a subdomain on another machine. I could try below, but its a production server and worry about messing it up. Hence the question. So I have machine names: mydomain.com (mail server) dev.mydomain.com currently the... (1 Reply)
Discussion started by: gtrava01
1 Replies

2. Windows & DOS: Issues & Discussions

gVim on windows 7 64 constantly overwrites symbolic and even hard links

I use sugarsync to sync my vimrc across computers. I keep the _vimrc file in a syncing folder and in my home folder, I have a symbolic link ~\_vimrc pointing to ~\Synced Docs\_vimrc. On my mac I have a .vimrc symbolic link pointing at the _vimrc file. On the pc side, every time I open the _vimrc... (3 Replies)
Discussion started by: dp88
3 Replies

3. Programming

pthread_mutex_trylock() overwrites global variable on CentOS5

Hi all, I am new to linux and got problem with pthread_mutex_trylock(). I have used mutex in my code. When I try to call pthread_mutex_trylock() on RECURSIVE type of mutex it overwrites adjacent memory location (that is global variable of type structure say x, memory allocated using malloc()). ... (5 Replies)
Discussion started by: liveshell
5 Replies

4. Shell Programming and Scripting

Cannot redirect ouput?????

Hello experts, I'm testing a program that prints error message to the screen. I want to redirect the output to a file using >. but the message only prints on screen and not writing to the file, Any suggestion on what I might try? (3 Replies)
Discussion started by: minifish
3 Replies

5. UNIX for Dummies Questions & Answers

Redirect to variable

how do you redirect stdout into a variable. whenever I try I get an ambiguous redirect error :( I am trying to validate some user input and failing miserably. cal $MONTH $YEAR | grep -c "$DAY" if the above is 1 then it is valid if 0 then not valid. I have been trying to redirect the output... (2 Replies)
Discussion started by: MrAd
2 Replies

6. UNIX for Dummies Questions & Answers

ambiguous redirect

i have following statement in the script echo -e "$str_XML_col_name:$str_field_type;" >> $i_DC_Key_$i_Tgt_DC_key_Schema here $i_DC_Key is DC key and $i_Tgt_DC_key are the variables............... when i ran the script i am getting error rec_merge.sh: $i_DC_Key_$i_Tgt_DC_key_Schema:... (1 Reply)
Discussion started by: mahabunta
1 Replies

7. Programming

Redirect Browser

Hello guys, Is it possible to redirect browser to another socket address? Cheers, Elton (1 Reply)
Discussion started by: EltonSky
1 Replies

8. Shell Programming and Scripting

Redirect question

Hi, I need some help to achive the follwoing task: I have a file named test that contain the following line: 'Hellow world','good morning' I want to attach the content of this file to a variable named var , and then rediarect it to a second file bamed test_new. The result should look like... (2 Replies)
Discussion started by: yoavbe
2 Replies

9. IP Networking

Redirect

I'm sittig behind a firewall that doesn't allow ftp. I have a conection to a UNIX system, connecting throug SSH. Is it possible to redirect the ftp through the UNIX to my computer? (1 Reply)
Discussion started by: <Therapy>
1 Replies

10. Shell Programming and Scripting

Bash overwrites data on screen!!

hi everybody, when i run and compile this: printf("test"); fflush(stdout); nothing appears on screen. if i try this: ___________________________________ printf("test"); fflush(stdout); sleep(10); ___________________________________ then i can see the output "test"... for 10... (4 Replies)
Discussion started by: brain_processin
4 Replies
Login or Register to Ask a Question