I've written a C program that works when I pipe data into my program using stdin like:
gunzip -c IN.gz|./a.out
If I want to run my program on a list of files I can do something like:
for i `cat list.txt`
do
gunzip -c $i |./a.out
done
But this will start my program 'number of files' times. I'm interested in piping all the files into the same process run.
Like doing
for i `cat list.txt`
do
gunzip -c $i >>tmp
done
cat tmp |./a.out
How can I do this?
This is rather a shell question. But AFAIK you can do:
or
If your program doesn't need to know when a particular input ends and another one begins, you can do this:
I hope it will help you Regards
xargs is your friend
% cat list.txt | xargs gunzip -c | ./a.out
if the files in list.txt have spaces in them then you need to go through some extra hoops.
There is no need for a shell loop:
With the '
-cd
' option, gzip will uncompress a list of files to standard output (or you can use 'gunzip -c
'). The$(<file)
notation expands the contents of the named file as a list of arguments without launching a sub-process. It is equivalent to$(cat list.txt)
otherwise.However, if you feel you must use a loop, then simply pipe the output from the loop into a single instance of your program:
If the contents of the loop are more complex (than simply gunzipping a single file), this might be necessary. You can also use '
{ ... }
' I/O redirection:Or:
Note the semi-colon; it is necessary with braces. In this example, it is essentially the same as using a formal sub-shell with parentheses:
Or:
Note the absence of a semi-colon here; it is not needed. The shell is wonderfully devious on occasion. The braces I/O redirection can be useful when you need to group commands after the pipe symbol:
You should be able get one
gunzip
process unzip multiple files.(
zcat
is another way of callinggunzip -c
on many systems and shows the parallel withcat
; but check forgzcat
if your system'szcat
is actuallyuncompress
.)Alternatively you can use a sub shell.