Search code examples
linuxshellunixcut

How can I pipe multiple arguments into a unix command?


I couldn't find anything similar hence posting the question.

Say I have set of commands which provides an output

First set which produces an output

cat xyx.txt | awk '{.........}' | sed 's/.../' | cut -d....   

Second set which produces an output

cat abc.txt | awk '{.........}' | cut -d ... | sed 's/...../'

I want the output of these as the 2 parameters to the "join" command. I know I can redirect these two a file and then use the join command with the files as the arguments.

Basically, can the whole thing be done in a single line.... something like

[first set of commands] > join -1 1 -2 1 < [second set of commands] 

Solution

  • If you are using bash, which is the default shell in many Linux distributions, the expression:

    join <([first set of commands]) <([second set of commands])
    

    is valid.

    So

    join <(cat xyx.txt | awk '{.........}' | sed 's/.../' | cut -d....) <(cat abc.txt | awk '{.........}' | cut -d ... | sed 's/...../')
    

    should make it.

    Basic example

    $ cat a
    1 hello
    2 bye
    3 ciao
    $ cat b
    1 hello 123
    2 bye 456
    3 adios 789
    $ cut -d' ' -f1,2 b | awk '{print $1, 2, $2}'
    1 2 hello
    2 2 bye
    3 2 adios
    $ join <(cut -d' ' -f1,2 b | awk '{print $1, 2, $2}') a
    1 2 hello hello
    2 2 bye bye
    3 2 adios ciao