Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use STDIN twice from pipe

I have a awk script something like

awk 'FNR==NR {col1[$1]++; col2[$2]++; next} {print $0, col2[$2] "/" length(col1)}' input input

But in case I have lot of files and need to use this script for concatenated files together like:

cat *all_input | awk 'FNR==NR {col1[$1]++; col2[$2]++; next} {print $0, col2[$2] "/" length(col1)}' STDIN STDIN

Does not work. How to use STDIN twice from pipe?

like image 670
Geroge Avatar asked Mar 08 '23 19:03

Geroge


1 Answers

You don't need to use a pipe. If you are using bash use process-substitution as <(cmd) i.e. to achieve a redirection where the input or output of a process (some sequence of commands) appear as a temporary file.

awk 'FNR==NR {col1[$1]++; col2[$2]++; next} {print $0, col2[$2] "/" length(col1)}' <(cut -f3 5- input) <(cut -f3 5- input)
like image 86
Inian Avatar answered Mar 19 '23 20:03

Inian