I'm trying pipe the output of an awk command through more than one command at once in a Bash shell, following my knowledge I'm coming up with this:
awk '$13 ~ /type/ {print $15}' filename.txt | (wc -l || sort -u)
I want the result of the awk command to be both counted AND sorted, how can I accomplish that? Even with && command it doesn't work, it does execute the first command and then exits. I guess it is my knowledge of bash which is failing.
Thanks in advance.
If you want to send output to two different commands in a single line, you'll need to do process substituion.
Try this:
awk '$13 ~ /type/ {print $15}' filename.txt | tee >(wc -l >&2) | sort -u
This outputs the line count on stderr and the sorted output on stdout. If you need the line count on stdout, you can do that leave off the >&2
, but then it will be passed to the sort call and (most likely) sorted to the top of the output.
EDIT: corrected description of what happens based on further testing.
in that case, do your counting in awk , why the need for pipes? don't make it more complicated
awk '$13 ~ /type/ {print $15;c++}END{print c} ' filename.txt | sort -u
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With