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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-27-2008, 01:18 PM
|
#1
|
Member
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36
Rep:
|
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.
|
|
|
06-27-2008, 08:24 PM
|
#2
|
Member
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36
Original Poster
Rep:
|
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?
|
|
|
06-27-2008, 11:48 PM
|
#3
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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.
|
|
|
06-28-2008, 12:39 AM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-29-2008, 12:17 AM
|
#5
|
Member
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36
Original Poster
Rep:
|
Quote:
Originally Posted by Mr. C.
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.
|
|
|
All times are GMT -5. The time now is 06:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|