Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paste without temporary files in Unix

I'm trying to use the Unix command paste, which is like a column-appending form of cat, and came across a puzzle I've never known how to solve in Unix.

How can you use the outputs of two different programs as the input for another program (without using temporary files)?

Ideally, I'd do this (without using temporary files):

./progA > tmpA; ./progB > tmpB; paste tmpA tmpB

This seems to come up relatively frequently for me, but I can't figure out how to use the output from two different programs (progA and progB) as input to another without using temporary files (tmpA and tmpB).

For commands like paste, simply using paste $(./progA) $(./progB) (in bash notation) won't do the trick, because it can read from files or stdin.

The reason I'm wary of the temporary files is that I don't want to have jobs running in parallel to cause problems by using the same file; ensuring a unique file name is sometimes difficult.

I'm currently using bash, but would be curious to see solutions for any Unix shell.

And most importantly, am I even approaching the problem in the correct way?


Cheers!

like image 828
Oliver Avatar asked Oct 15 '09 00:10

Oliver


3 Answers

You do not need temp files under bash, try this:

paste <(./progA) <(./progB)

See "Process Substitution" in the Bash manual.

like image 166
Balázs Pozsár Avatar answered Oct 24 '22 13:10

Balázs Pozsár


Use named pipes (FIFOs) like this:

mkfifo fA
mkfifo fB
progA > fA &
progB > fB &
paste fA fB
rm fA fB

The process substitution for Bash does a similar thing transparently, so use this only if you have a different shell.

like image 45
Cristian Ciupitu Avatar answered Oct 24 '22 15:10

Cristian Ciupitu


Holy moly, I recent found out that in some instances, you can get your process substitution to work if you set the following inside of a bash script (should you need to):

set +o posix

http://www.linuxjournal.com/content/shell-process-redirection

From link: "Process substitution is not a POSIX compliant feature and so it may have to be enabled via: set +o posix" I was stuck for many hours, until I had done this. Here's hoping that this additional tidbit will help.

like image 2
Kevin Lee Avatar answered Oct 24 '22 14:10

Kevin Lee