LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2005, 06:35 PM   #1
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Rep: Reputation: 15
redirect AND print stdout


Hello!

I would like to redirect stdout to a file (or pipe it) AND display stdout.

something like
$cat whatever.txt > file.txt magicOperatorForStdout

so that stdout is being "split" or copied so I can use it in many places.

thanks!
 
Old 08-02-2005, 07:41 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.
Code:
$cat whatever.txt | tee file.txt
Dave
 
Old 08-04-2005, 10:23 AM   #3
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Original Poster
Rep: Reputation: 15
thanks! but is there also a way to pipe "one of the stdouts" so that "the real stdout" is being printed, but the other is being piped to grep for instance, and later on redirected to a file?
 
Old 08-04-2005, 10:47 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
I'm sure there's an easier way, but I can't think of it at the moment, however, try this:
Code:
mkfifo greppipe
grep myregexp greppipe > grepout &
cat whatever.txt | tee greppipe
rm greppipe
That should give you the results of the grep in 'grepout' and stdout should be printed to (un-grepped) as well.

Dave
 
Old 08-04-2005, 12:44 PM   #5
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Original Poster
Rep: Reputation: 15
thanks! I didn't know about the mkfifo command. pretty sweet =)
 
Old 08-04-2005, 09:56 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Welcome to the power of UNIX... :)
 
Old 08-04-2005, 10:02 PM   #7
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
narg, I was going to post about tee, but I realized it was already posted.

BTW, This will write both stdout and stderr to the file allfile.txt
Code:
(cat whatever.txt) 2>&1 | tee log.txt
However, how is there a way to split stderr to one file and stdout to another file. Both must be printed to the screen normally.

Last edited by AngryLlama; 08-04-2005 at 10:13 PM.
 
Old 08-04-2005, 10:36 PM   #8
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
Nevermind. I finally figured it out:

Code:
(cat whatever.txt | tee log.txt) 3>&1 1>&2 2>&3 | tee err.txt 3>&1 1>&2 2>&3
of course, thats worthless if using cat.. but the idea is the same
 
Old 08-07-2005, 05:30 AM   #9
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by AngryLlama
However, how is there a way to split stderr to one file and stdout to another file. Both must be printed to the screen normally.
Code:
someprog 2>err.log 1>out.log
Neither is printed to the screen.
 
Old 08-09-2005, 05:01 PM   #10
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
Thanks--

However,
Code:
(someprog | tee log.txt) 3>&1 1>&2 2>&3 | tee err.txt 3>&1 1>&2 2>&3
will seperate stderr and stdout to seperate files while still printing both to the screen.

So, it looks like we have every possible redirect scenerio posted on this thread
 
Old 09-28-2012, 03:50 PM   #11
amseek
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by AngryLlama View Post
So, it looks like we have every possible redirect scenerio posted on this thread
-- which is awesome thank you
 
Old 09-30-2012, 08:42 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
We're happy you're happy, but (as the reply page you had to go through to post it undoubtedly pointed out), please don't reopen old threads unless you have something substantial to add to that discussion.

Speaking of which, I can add something to it. bash now offers process substitution, which can usually be used with tee instead of the old cumbersome redirectors.

Example:
Code:
command 2> >( grep "error" >errorfile ) | tee >( grep "output" >outputfile )
stderr is filtered by the first grep and saved to a file.
stdout is split by tee.
One stream is filtered by the second grep and then to a file,
while the raw data goes to screen.

PS: I could swear I came across an almost identical reopened thread just a month or two ago.

Last edited by David the H.; 09-30-2012 at 08:43 AM. Reason: minor clarification
 
Old 10-07-2012, 05:37 PM   #13
amseek
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
PS: I could swear I came across an almost identical reopened thread just a month or two ago.
'twasn't me sorry about the old post thing.. didn't notice it was from 2005! whoops. sorry

thanks for the update though and the page itself of course

-Art
 
  


Reply



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
TCSH: redirect stdout and stderr seperately ugenn Linux - General 3 02-07-2016 03:23 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM
how to redirect stdout [binary stream] to multiple processes vtaminh Linux - General 2 08-19-2004 01:05 PM
Redirect print output to a folder instead of printer? xmdms Linux - Enterprise 1 07-31-2004 10:11 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 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
Open Source Consulting | Domain Registration