简体   繁体   中英

Writing to multiple files with awk

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 DI would end up with something like:

output_file_A.txt
output_file_B.txt
output_file_C.txt
output_file_D.txt

Thanks.

Carlos

你可以使用这个awk

awk 'BEGIN {FS="\t"}; {fn="output_file_" $5 ".txt"} {print $2,FS,$3,FS,$4 > fn} input_files*.txt
 awk 'BEGIN {FS="\t"}; {print $2,FS,$3,FS,$4 >> "output_file_"$5} input_files*.txt

awk的重定向与终端完全相同。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM