LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-27-2008, 01:18 PM   #1
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Rep: Reputation: 15
Directing Script output to Memory but without using a pipe |


I'm trying to run a series of commands to read a live data dump an strip it in various ways on the fly and send those various stripped pieces of data to corresponding files.

For instance I have the following:

Code:
./dumpscript.sh | sed '/print/,/raw/d' | cut -c 1-47 > output.txt
In this case my dumpscript.sh output is piped through "sed" and "cut" to give me the specific stream of data that I need for archiving.

Now at the same time I would like to run a second "cut" with different parameters and output the results to another file. Again this needs to be live on the fly.

I've tried:

Code:
./dumpscript.sh | sed '/print/,/raw/d' | cut -c 1-47 > output.txt ; cut -c 1-18 > output2.txt
However, there's nothing in the second output file.

Any help would be appreciated.
 
Old 06-27-2008, 08:24 PM   #2
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Is there some way that I could use "tee" perhaps? I read in the man page that:

Code:
The `tee' command copies standard input to standard output and also to any files given as arguments.  This is useful when you want not only to
send some data down a pipe, but also to save a copy.
Any suggestions?
 
Old 06-27-2008, 11:48 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The problem with your last command line is that the semicolon is a command separator. Its just like entering the first command, hitting Enter, and then the second.

What you need is a way to send output to a file... and use tee to send the same stream of bytes to another pipeline. The problem is that you cannot both redirect (>) and pipe (|) STDOUT in the same command.

However, this is where sub-shells are useful. There is nothing to prevent you from sending your output to another shell, which can then redirect away. Try this example:
Code:
$ echo Help Me | sed 's/Me/You/' | tee file1 | (cut -c1-4 > file2)
$ cat file1
Help You
$ cat file2
Help
Note that the STDOUT for the current shell is sent along a pipeline, but the STDOUT of the sub-shell is redirected.

Last edited by Mr. C.; 06-29-2008 at 12:19 AM.
 
Old 06-28-2008, 12:39 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How about awk?

Code:
./dumpscript.sh | sed '/print/,/raw/d' | awk '{printf substr($0,1,47) > "output1.txt"; printf substr($0,1,18) > "output2.txt"}'


Cheers,
Tink
 
Old 06-29-2008, 12:17 AM   #5
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by Mr. C. View Post
The problem with your last command line is that the semicolon is a command separator. Its just like entering the first command, hitting Enter, and then the second.

What you need is a way to send output to a file... and use tee to send the same stream of bytes to another pipeline. The problem is that you cannot both redirect (>) and pipe (|) STDOUT in the same command.

However, this is where sub-shells are useful. There is nothing to prevent you from sending your output to another shell, which can then redirect away. Try this example:
Code:
$ echo Help Me | sed 's/Me/You/' | tee file1 | (cut -c1-4 > file2)
$ cat file1
Help You
$ cat file2
Help
Note that the STDOUT the current shell is sent along a pipeline, but the STDOUT of the sub-shell is redirected.
Mr. C.

Thank you so much, I really appreciate your help... That works exactly as I would like, I can't wait to try it on a live system on Monday, It will be far more efficient than my current methods which require quite a bit of manual work.

Thanks, again.
 
  


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
Directing sound output to microphone Night Ink Linux - Software 2 12-28-2007 12:52 AM
How can I pipe output of a command into arguments of a script HGeneAnthony Linux - General 3 12-26-2007 06:24 AM
Directing xosd output to X display from crontab BWebb Linux - General 3 06-30-2005 01:09 PM
how to pipe output from make BBB Programming 2 05-20-2005 06:14 AM
Output truncated when using pipe tells Linux - General 1 12-16-2003 08:06 PM

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

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