Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

paste two outputs

Posted on 2002-06-22
9
Medium Priority
?
237 Views
Last Modified: 2010-04-21
In order to paste two outputs from two commands, I have
to re-direct the outputs to 2 temporary files and then paste
them together, such as,
  cmd1 > tmpfile1
  cmd2 > tmpfile2
  paste tmpfile1 tmpfile2 > the_wanted_results

Can some one think of way to elimilate the use of
the temporay files, i.e., make it one liner instead
of using three lines.
0
Comment
Question by:geermu
9 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 7101138
That's easy, do it like so:

cmd1 >wanted_results
cmd2 >>wanted_results

On the first command the single redirection operator (>) created a new file to hold what cmd1 produced on STDOUT. The second line used the append redirection operator that appends data to an existing file (and on most Unixen it will create the file if it doesn't exist_. So in general it is safe to do:

cmd1 >>wanted_results
cmd2 >>wanted_results
cmd3 >>wanted_results
0
 

Expert Comment

by:hotbox
ID: 7101222
yes,use >> to append your others output to same file.
0
 

Author Comment

by:geermu
ID: 7101294
No, it is not append.
It is paste.
I have increased the points. I guess it may need to
combine sub-shell and pipe to do it.
Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 7102095
(cmd1; cmd2)>the_wanted_results
0
 
LVL 21

Expert Comment

by:tfewster
ID: 7103606
I doubt you can do this from the command line, as:
a) cmd1 will put a carriage return at the end of each output line, so the output from cmd2 will appear on the next line instead of tab-separated;
b) cmd1 & cmd2 are not synchronised, so cmd1 may write 2 lines of output before cmd2 writes its first output.

Of course, it depends on the commands. If you tell us more about what you are trying to do, we may be able to suggest an alternative.

0
 
LVL 5

Accepted Solution

by:
ecw earned 600 total points
ID: 7112358
in ksh...
cmd1 |& exec 4>&p 3<&p cmd2 |& while read line1 <&3 && read line2 <&p ; do echo "$line1 $line2" ; done ; cat <&3 ; cat <&p ; exec 4>&- 4<&-

all one line, seriously though what you're tryng to do can't be done unless your OS has /dev/fd support and your shell does not gratuitously close fd's when exec'ing a command.  IIRC, bourne shell leaves all fd's open, ksh and its kin close all but std{in,out,err}.
0
 
LVL 85

Expert Comment

by:ozo
ID: 7112474
Would you settle for two lines and 1 temporary file?
0
 
LVL 9

Expert Comment

by:PeterMac
ID: 7126970
Sorry missed this earlier,

paste 'cmd1' 'cmd2' > result_file

quotes around cmd1 & cmd2 are backquote, execute in situ, and replace with standard out from comand.
0
 
LVL 5

Expert Comment

by:ecw
ID: 7127207
 paste [-s] [-d list] file...
Hmm. I don't think "paste 'cmd1' 'cmd2' > result_file" will do.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question