用awk过滤文件并将标头保留在输出中 [英] Filter file with awk and keep header in output
本文介绍了用awk过滤文件并将标头保留在输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下CSV文件:
a,b,c,d
x,1,1,1
y,1,1,0
z,1,0,0
我要保留多于1行的行,因此我执行以下awk命令:
I want to keep lines that add up more than 1, so I execute this awk command:
awk -F "," 'NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file
并获得此信息:
x,1,1,1
y,1,1,0
我该怎么做,但保留第一行(标题)?
How can I do the same but retain the first line (header)?
推荐答案
$ awk -F "," 'NR==1; NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file
a,b,c,d
x,1,1,1
y,1,1,0
这篇关于用awk过滤文件并将标头保留在输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文