AWK删除带有两个空字段的CSV行?

时间:2015-04-30 19:15:58

标签: bash csv awk

我很少使用awk,我想我忘记了使用CSV文件的基本知识,这里,但我有以下文件,名为new2.csv:

Filename,Dist.(ft),BR Name,BR 2-Way Time,Till/WBR Name,Till/WBR 2-Way Time
FILE____007P_1.DZT,0,BR,,Till/WBR,
FILE____007P_1.DZT,0.042,BR,,Till/WBR,
FILE____007P_1.DZT,0.083,BR,,Till/WBR,
FILE____007P_1.DZT,0.125,BR,,Till/WBR,
FILE____007P_1.DZT,0.167,BR,,Till/WBR,
FILE____007P_1.DZT,0.208,BR,92.58,Till/WBR,
FILE____007P_1.DZT,0.25,BR,,Till/WBR,29.3
FILE____007P_1.DZT,0.292,BR,,Till/WBR,

我想只使用awk保留第四列或第六列(第7行和第8行)中包含值的行。

我尝试了一些事情来检查那里发生了什么:

awk -F',' '{print NR, "->", $4}' new2.csv

返回第7行 - > 7号线为92.58,其余线路没有,所以这很好。接下来,我试过

awk -F',' '{print NR, "->", $6}' new2.csv

返回第8行 - > 29.3,所以我们仍然很好。

认为我已经解决了,我继续

awk -F',' '$4!=""' new2.csv

并按预期打印标题行和代码的第七行。继续到第6列,我写了相同的表达式,它返回new2.csv的全部内容。在尝试排除故障时,我尝试

awk -F',' '{print NR, "->", $6!=""}' new2.csv

并返回第1行 - > 1,line2 - > 1,...,第8行 - > 1,......等等,所以我的问题就出现了。发生了什么事?有没有办法解决它?

该行末尾的逗号似乎可能是问题的根源,但在阅读了相当多的帖子后,我仍然不确定如何处理它。 awk '{print substr($0,0,length($0)-1)}' new.csv也不会删除最后一个逗号。我在Windows 8机器上生成了csv,并在bash的Linux机器上使用了awk。

2 个答案:

答案 0 :(得分:2)

$ awk -F, '($4$6)~/./' file
Filename,Dist.(ft),BR Name,BR 2-Way Time,Till/WBR Name,Till/WBR 2-Way Time
FILE____007P_1.DZT,0.208,BR,92.58,Till/WBR,
FILE____007P_1.DZT,0.25,BR,,Till/WBR,29.3

或者如果你想要只考虑包含空格的字段"空"太:

$ awk -F, '($4$6)~/[^[:space:]]/' file
Filename,Dist.(ft),BR Name,BR 2-Way Time,Till/WBR Name,Till/WBR 2-Way Time
FILE____007P_1.DZT,0.208,BR,92.58,Till/WBR,
FILE____007P_1.DZT,0.25,BR,,Till/WBR,29.3

如果你想排除标题行:

$ awk -F, '(NR>1) && (($4$6)~/[^[:space:]]/)' file
FILE____007P_1.DZT,0.208,BR,92.58,Till/WBR,
FILE____007P_1.DZT,0.25,BR,,Till/WBR,29.3

答案 1 :(得分:1)

Filename,Dist.(ft),BR Name,BR 2-Way Time,Till/WBR Name,Till/WBR 2-Way Time FILE____007P_1.DZT,0,BR,,Till/WBR,
FILE____007P_1.DZT,0.042,BR,,Till/WBR,
FILE____007P_1.DZT,0.083,BR,,Till/WBR,
FILE____007P_1.DZT,0.125,BR,,Till/WBR,
FILE____007P_1.DZT,0.167,BR,,Till/WBR,
FILE____007P_1.DZT,0.208,BR,92.58,Till/WBR,
FILE____007P_1.DZT,0.25,BR,,Till/WBR,29.3
FILE____007P_1.DZT,0.292,BR,,Till/WBR,

hzhang@dell-work ~ $ cat test.awk 
#!/usr/bin/awk
BEGIN{
    FS = ","
}
{
    # ignore the first definition line
    if(FNR > 1){
        # checks column 4 has a non-empty value
        if($4 !=""){
            print FNR,"->", $4
        }

        # checks column 6 has a non-empty value
        if($6 != ""){
            print FNR,"->", $6
        }

    }
}
hzhang@dell-work ~ $ awk -f test.awk sample.csv 
6 -> 92.58
7 -> 29.3

如果要在控制台上运行命令行:

hzhang@dell-work ~ $ awk -F, '(FNR>1){if($4 != ""){ print FNR,"->",$4  }; if($6 != ""){ print FNR,"->",$6  }}' sample.csv 
6 -> 92.58
7 -> 29.3