Reputation: 391
I have some text files which have several columns. I'm using awk to only write the columns I want into an output file which I later analyze.
awk 'BEGIN {FS="\t"}; {print $2,FS,$3,FS,$4} input_files*.txt >> output_file.txt
Can I go one step farther and redirect the output of my awk command to more than one file depending on the value of let's say column $5? If column $5 = A, B, C and D I would end up with something like:
output_file_A.txt
output_file_B.txt
output_file_C.txt
output_file_D.txt
Thanks.
Carlos
Upvotes: 4
Views: 6996
Reputation: 26667
awk 'BEGIN {FS="\t"}; {print $2,FS,$3,FS,$4 >> "output_file_"$5} input_files*.txt
The redirection within awk
works exactly like the terminal.
Upvotes: 7
Reputation: 785256
You can use this awk
:
awk 'BEGIN {FS="\t"}; {fn="output_file_" $5 ".txt"} {print $2,FS,$3,FS,$4 > fn} input_files*.txt
Upvotes: 3