bashunix

How can I split the stdout from a program into two (multiple) streams based on some grep commands?


I have a program that dumps different tabular data formats (number of columns and column types) to stdout; ideally, I want to run the program only once and split stdout into multiple streams, so I can read the CSVs directly from the streams, without attempting to homogenize the tabular data or run the program multiple times.


Solution

  • For a smallish number of outputs, you could use an awk that supports /dev/fd/# instead of grep:

    dump_program |
    awk '
        /regex1/ { print > "/dev/fd/1" }
        /regex2/ { print > "/dev/fd/2" }
        /regex3/ { print > "/dev/fd/3" }
        # ...
    ' \
        1> >( process1 ) \
        2> >( process2 ) \
        3> >( process3 ) \
    ;