Splitting then merging standard input in one operation
Of course, this could be used on standard input like output of any command, as well as on a file.
This demonstration use command output directly, without the requirement of temporary file.
First, the bunch of lines:
I've condensed your 1st tmp file into this one line command:
. <(printf 'printf "%s %%d\n" {1..10};' foo bar baz)
For reducing output on SO, here is a sample of output for 3 lines by word (rest of this post will still use 10 values per word.):
. <(printf 'printf "%s %%d\n" {1..3};' foo bar baz)
foo 1
foo 2
foo 3
bar 1
bar 2
bar 3
baz 1
baz 2
baz 3
You will need a fifo for the split:
mkfifo $HOME/myfifo
Note: this could be done by using unnamed fifo (aka without temporary fifo), but you have to manage openning and closing file descriptor by your script.
tee for splitting, then paste for merging output:
Quick run:
. <(printf 'printf "%s %%d\n" {1..10};' foo bar baz) |
tee >(grep foo >$HOME/myfifo ) | grep ba |
paste -d $'\1' $HOME/myfifo - - | sed 's/\o1/ and /g'
(Last sed is just for cosmetic) This should produce:
foo 1 and bar 1 and bar 2
foo 2 and bar 3 and bar 4
foo 3 and bar 5 and bar 6
foo 4 and bar 7 and bar 8
foo 5 and bar 9 and bar 10
foo 6 and baz 1 and baz 2
foo 7 and baz 3 and baz 4
foo 8 and baz 5 and baz 6
foo 9 and baz 7 and baz 8
foo 10 and baz 9 and baz 10
With some bash script in between:
. <(printf 'printf "%s %%d\n" {1..10};' foo bar baz) | (
tee >(
while read -r word num;do
case $word in
foo ) echo Word: foo num: $num ;;
* ) ;;
esac
done >$HOME/myfifo
) |
while read -r word num;do
case $word in
ba* ) ((num%2))&& echo word: $word num: $num ;;
* ) ;;
esac
done
) | paste $HOME/myfifo -
Should produce:
Word: foo num: 1 word: bar num: 1
Word: foo num: 2 word: bar num: 3
Word: foo num: 3 word: bar num: 5
Word: foo num: 4 word: bar num: 7
Word: foo num: 5 word: bar num: 9
Word: foo num: 6 word: baz num: 1
Word: foo num: 7 word: baz num: 3
Word: foo num: 8 word: baz num: 5
Word: foo num: 9 word: baz num: 7
Word: foo num: 10 word: baz num: 9
Other syntax, same job:
paste $HOME/myfifo <(
. <(printf 'printf "%s %%d\n" {1..10};' foo bar baz) |
tee >(
while read -r word num;do
case $word in
foo ) echo Word: foo num: $num ;;
* ) ;;
esac
done >$HOME/myfifo
) |
while read -r word num;do
case $word in
ba* ) ((num%2))&& echo word: $word num: $num ;;
* ) ;;
esac
done
)
Removing fifo
rm $HOME/myfifo
pasteor somesuch rather thancatif you want to get foo and bar on the same line infile_tmpgrep fooandgrep barbut then only testif [ $word = "foo" ]? What'sbargot to do with it in that case?ifpart of the code was just part of the example I made up to illustrate how my actual problem requires doing something on the second column based on the content of the first. What this code does exactly is silly, I know. Actually I added theifthing altogether at the very end of my edits before publishing the question. I should have discarded it as it distracts from my actual problem.