简体   繁体   English

如何在awk中使用循环

[英]how to use loop in awk

I use the following command to extract 2nd column from table1.txt and get output as output1.txt 我使用以下命令从table1.txt提取第二列,并输出为output1.txt

awk '{ print $2 }' table.txt > output.txt

How to use loop for five files ( table.txt , abc.txt , pqr.txt , skt.txt , mkt.txt ) to extract 2nd column in respective output files ( out_table.txt , out_abc.txt , out_pqr.txt , out_skt.txt , out_mkt.txt ) ? 如何使用五个文件( table.txtabc.txtpqr.txtskt.txtmkt.txt )的mkt.txt来提取各个输出文件( out_table.txtout_abc.txtout_pqr.txtout_skt.txt )中的第二列out_skt.txtout_mkt.txt )?

you don't need to write a loop in awk. 您无需在awk中编写循环。 You could use the build-in variable FILENAME : 您可以使用内置变量FILENAME

awk '{print $2 > "out_"FILENAME".txt"}' table.txt abc.txt pqr.txt skt.txt mkt.txt

使用FILENAME变量将相应的输出文件名用于输入一个:

awk '{ print $2 > "out_" FILENAME }' *.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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