删除少于7列且空第二列的行

时间:2015-01-14 12:28:36

标签: bash awk

我需要从csv中删除所有少于7个字段的行以及第二列为空的行

V8, 5,2015-01-14,3.7,-0.8,0.2,7.2
CT,,,,,,
R1, 6,2015-01-14,9.7,-1.0,0.0,14.4
Z8, 5 ,2015-01-14,1.4,-4.0,0.1,54.4
XL, 5,2015-01-14,15.0,6.4,0.0,27.7
D9, 6,2015-01-14,16.4,2.6,0.0,34.6
XM, 6,2015-01-14,2.2,-0.2,0.2,9.0
ZC, 5 ,2015-01-14,
,
,

我有7个字段(列)

awk -F',' '{print NF; exit}' 2015-01-14.csv 
7

我尝试了下一个命令,以便删除少于7个字段/列的行,但不运行

awk -F"," 'NF>=7' 2015-01-14.csv 

任何帮助?

1 个答案:

答案 0 :(得分:2)

您可以尝试下面的内容,

$ awk -F, 'NF>=7 && $2 != ""' file
V8, 5,2015-01-14,3.7,-0.8,0.2,7.2
R1, 6,2015-01-14,9.7,-1.0,0.0,14.4
Z8, 5 ,2015-01-14,1.4,-4.0,0.1,54.4
XL, 5,2015-01-14,15.0,6.4,0.0,27.7
D9, 6,2015-01-14,16.4,2.6,0.0,34.6
XM, 6,2015-01-14,2.2,-0.2,0.2,9.0

这将打印包含7个或更多字段的行,第二个字段也不会为空。