Reputation: 1789
I'm receiving as input a stream of commands:
command1
command2
command3
From this, I would like to create a file output.txt
containing:
command1 output1
command2 output2
command3 output3
Where the output_i
is the output of command_i
(each command returns a single integer). I can do that using parallel
and paste
successively, but I was wondering whether there is a way to get output.txt
in a single bash call.
EDIT
with parallel
, this is how I do it:
cat commands.txt | parallel -k > outputs_only.txt
paste commands.txt outputs_only.txt > outputs.txt
Upvotes: 1
Views: 49
Reputation: 85550
Just a loop in bash
with input-redirection on the file containing the commands,
#!/bin/bash
while read -r line; do
echo "$line" "$(eval "$line")"
done < commands.txt > output.txt
Or in a single-line as
while read -r line; do echo "$line" "$(eval "$line")"; done < commands.txt > output.txt
In case you want to read from stdin
and not from a file, just pipe the stream to the loop,
< command-producing-stream > | while read -r line; do echo "$line" "$(eval "$line")"; done > output.txt
Upvotes: 2