简体   繁体   English

LINUX awk遍历txt文件的多行

[英]LINUX awk loop through several lines of a txt file

I need to loop through several rows of a text file and redirect to another text file. 我需要遍历文本文件的几行,然后重定向到另一个文本文件。

I managed to do the following: 我设法做到以下几点:

for i in $(seq 1 24)
awk 'NR==1 {print $4 $5 $6}'  3dfwhmx.txt > values.txt
values='cat*values.txt'
3dClust -fwhmxyz ${values}
done

However this only address the first row , 4,5,6th column . 但是,这仅处理first row 4,5,6th column I would need to use the i index in order to address the 1st, 2nd, 3rd etc row . 我将需要使用i索引来寻址1st, 2nd, 3rd etc row I do need to create a vector of values for each iteration of i. 我确实需要为i的每次迭代创建一个值向量。

I tried several things eg; 我尝试了几件事,例如;

awk 'NR=='$i'  {print $4 $5 $6}'  3dfwhmx.txt > values.txt

but it does not work. 但它不起作用。 I would really appreciate any feedback on this! 我对此表示感谢!

do you need to loop through it and call awk for each line? 您是否需要遍历它并为每行调用awk?

Why not just 为什么不只是

awk '{print $4}'  3dfwhmx.txt > values.txt

or change your quotes to allow the shell substitution 或更改引号以允许替换外壳

awk "NR==$i {print $4}"  3dfwhmx.txt > values.txt

You don't need a loop around awk . 您不需要在awk周围循环。

awk 'NR<=24{print $4}'  3dfwhmx.txt > values.txt

will get you the first 24 lines as your logic with loop does. 与循环逻辑一样,您将获得前24行。


With the updated question, you don't need a temporary file either. 有了更新的问题,您也不需要临时文件。

for i in $(seq 1 24); do
   values=$(awk -v i=$i 'NR==i {print $4 $5 $6}'  3dfwhmx.txt)
   3dClust -fwhmxyz ${values}
done

Depending on how 3dClust works, you might be able to simple redirect `awk's output to its stdin using a pipe. 根据3dClust工作方式,您也许可以使用管道将`awk的输出简单重定向到其stdin。

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

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