LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-08-2012, 09:36 PM   #1
dspjm
Member
 
Registered: Dec 2010
Distribution: Scientific Linux Slackware
Posts: 91

Rep: Reputation: 2
Smile how to redirect input and output to one file


Suppose I want to manipulate a file foo, using commands like tr
I enter
tr a b <foo >foo
or
tr a b <foo | cat >foo
or
cat foo | tr a b >foo

As you know they all fail as all the contents of foo has been discarded
for the first command I enter, I guess it's because that when the tr command open foo for write, the foo has been overwritten. so the command can read nothing
for two pipelines, I thought it was the same reason.

So, is there a method to accomplish the task I want? or I have to use a tmp file?
 
Old 12-08-2012, 10:16 PM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
You might to trying in terms of your goal. You want to end up with a file named foo, that contains the changed lines of the original file named foo.
That's not the same as saying you have to use a file named foo as both the input and output file in the same command. Instead you could
change your command lines to look like these:

tr a b <foo >foo.new ; mv foo.new foo

tr a b <foo | cat >foo.new ; mv foo.new foo

cat foo | tr a b >foo.new ; mv foo.new foo
 
  


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
Redirect output sound to input (mic or line-in) chabacha Linux - Software 1 01-22-2011 03:15 PM
Redirect output sound to input (mic or line-in) chabacha Debian 1 01-22-2011 08:51 AM
how to redirect the output of find command to input of egrep babu198649 Linux - Newbie 2 12-27-2007 04:50 AM
bash input/output redirect problem greno Programming 2 12-31-2006 04:23 PM
Redirect /dev/ttyS0 input/output to tty2 Kymbo Linux - General 1 09-07-2006 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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