0

I use the following command to extract 2nd column from table1.txt and get output as 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) ?

4

2 回答 2

4

您不需要在 awk 中编写循环。您可以使用内置变量FILENAME

awk '{print $2 > "out_"FILENAME".txt"}' table.txt abc.txt pqr.txt skt.txt mkt.txt
于 2013-08-01T21:18:24.847 回答
3

使用FILENAME变量来使用对应的输出文件名来输入一个:

awk '{ print $2 > "out_" FILENAME }' *.txt
于 2013-08-01T21:17:22.830 回答