打印每n行成使用行GAWK [英] print every nth line into a row using gawk
本文介绍了打印每n行成使用行GAWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有,我需要得到每个第n行,并将其打印成一排非常巨大的文件。
我的数据:
1 937 4.320194
2 667 4.913314
3 934 1.783326
4 940 -0.299312
5 939 2.309559
6 936 3.229496
7 611 -1.41808
8 608 -1.154019
9 606 2.159683
10 549 0.767828
我希望我的数据是这样的:
1 937 4.320194
3 934 1.783326
5 939 2.309559
7 611 -1.41808
9 606 2.159683
这当然是一个例子,我要为我的庞大的数据文件中的每个10号线。我想这至今:
NF == 6 {
如果(NR%10){打印;}
}
解决方案
要打印每一第二行,从第一个:
的awk'NR%2 == 1'file.txt的
要打印每十行,从第十行:
的awk'NR%10 == 0'file.txt的
要在脚本中使用此,以下内容添加到一个名为 script.awk
:
BEGIN {
打印文件处理
}NR%10 == 0结束 {
打印处理完毕
}
然后执行:
的awk -f script.awk file.txt的
I have a very huge file in which I need to obtain every nth line and print it into a row.
My data:
1 937 4.320194
2 667 4.913314
3 934 1.783326
4 940 -0.299312
5 939 2.309559
6 936 3.229496
7 611 -1.41808
8 608 -1.154019
9 606 2.159683
10 549 0.767828
I want my data to look like this:
1 937 4.320194
3 934 1.783326
5 939 2.309559
7 611 -1.41808
9 606 2.159683
This is of course an example, I want every 10th line for my huge data file. I tried this so far:
NF == 6 {
if(NR%10) {print;}
}
解决方案
To print every second line, starting with the first:
awk 'NR%2==1' file.txt
To print every tenth line, starting with the tenth line:
awk 'NR%10==0' file.txt
To use this in a script, add the following to a file called script.awk
:
BEGIN {
print "Processing file"
}
NR%10==0
END {
print "Finished processing"
}
Then execute:
awk -f script.awk file.txt
这篇关于打印每n行成使用行GAWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文