awk gzip output to multiple files

2019-03-03 09:54发布

问题:

I can gzip an awk output like this:

echo "col1 col2" | awk '{print $1}' | gzip > out

But is it also possible to pipe two awk output file streams to gzip? This doesn't work:

echo "col1 col2" | awk '{print $1 | gzip > "out1.gz"; print $2 | gzip > "out2.gz"}'

回答1:

Using awk, it can handle more columns easily.

echo "col1 col2" | awk '{for (i=1;i<=NF;i++) print $i | "gzip > out" i ".gz"}'


回答2:

It is, and I found the correct syntax, when searching for awk pipe output:

echo "col1 col2" | awk '{print $1 | "gzip > out1.gz"; print $2 | "gzip > out2,gz"}'


标签: awk gzip