Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a shell (bash) How can I execute more than one command in a pipeline?

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.

like image 922
OverLex Avatar asked Dec 29 '22 09:12

OverLex


2 Answers

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.

like image 81
Walter Mundt Avatar answered Dec 31 '22 14:12

Walter Mundt


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
like image 23
ghostdog74 Avatar answered Dec 31 '22 14:12

ghostdog74