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 11-07-2008, 10:40 AM   #1
viroos
LQ Newbie
 
Registered: Dec 2005
Posts: 5

Rep: Reputation: 0
pipes and streams and one file


Hi,
I'm writing little Sed wrapper. It's task is to replace text. I have to override original file.

so my first try was:
Code:
#!/bin/bash
sed s/$1/$2/g <$3 >$3
and i got empty file. Hire is my first question: why for commend like this:
Code:
./my_script foo bar file
and file like this
Code:
foo
foo
foo
file is empty instead of one "bar" word?

Anyway I fixed this with:
Code:
#!/bin/bash
sed s/$1/$2/g <$3|cat >$3
And It works! But I'm not sure why? I think I should get only one bar word because when first line is outputted the input file is maked.

The second approach works but maybe I should better use tmp file?

Sorry for my poor English. And TIA for answers.
 
Old 11-07-2008, 10:52 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,117
Blog Entries: 2

Rep: Reputation: 113Reputation: 113
yes it's better to use a temp file in this case. when you use > it wipes the file you are writing to and since you were reading and writing from the same file the contents were getting wiped before you did anything with them. The pipe to cat only worked because the file was small. If it had been larger than one buffered read I'd bet you see similar problems to your first attempt.
 
Old 11-07-2008, 01:56 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And in addition to the temp-files idea:

newer GNU seds (> 3) allow in-file changes.

Code:
sed -i.bak s/$1/$2/g $3
with an optional back-up file created.


Cheers,
Tink
 
  


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
Curious about open file handlers and pipes with a past problem helptonewbie Linux - Newbie 1 08-27-2008 04:49 AM
how to add "pipes" to a sed program file new_2_unix Linux - Newbie 2 10-07-2007 10:09 PM
LXer: CLI tricks: UNIX Pipes, Streams and Redirections LXer Syndicated Linux News 0 09-16-2007 04:10 PM
Send ALL output streams to file? Nuvious Linux - General 1 04-22-2007 03:08 PM
about pipes kpachopoulos Programming 1 10-15-2005 01:37 PM


All times are GMT -5. The time now is 06:28 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