如何在使用awk的条件下删除csv文件行?

时间:2014-09-02 08:49:48

标签: linux awk

我得到了带有3列的file.csv,如下所示:

"/cw-0yLGNekxk4Vup/cwdd",-112.263,1
"001aCqAT2O+SbZfeC7UmhQ",,0
"/001DLIWmCvBwVCJrQjiQA",310,0
"fasdDLIWmCvBwVCJrQjiQA",0,0
...

但有些行没有列2值或0列2值我想删除这些行来获取:

"/cw-0yLGNekxk4Vup/cwdd",-112.263,1
"/001DLIWmCvBwVCJrQjiQA",310,0
...

如何在Ubuntu中使用awk执行此操作?

3 个答案:

答案 0 :(得分:1)

读错了问题

在awk中删除行

awk -F, 'NF!=3' file

实际上这是更好的

awk -F, '$2' file

Incase $ 2可以是0

awk -F, '$2!=""' file
awk -F, '$2~/./' file

答案 1 :(得分:1)

为什么不考虑使用 grep

grep -v '^[^,],,'  file

答案 2 :(得分:0)

你可以尝试下面的awk命令,

$ awk '/",,/{next}{print}' file
"/cw-0yLGNekxk4Vup/cwdd",-112.263
"/001DLIWmCvBwVCJrQjiQA",310

OR

$ awk -F, 'NF>2{next;}{print}' file
"/cw-0yLGNekxk4Vup/cwdd",-112.263
"/001DLIWmCvBwVCJrQjiQA",310

OR

$ awk -F, 'NF<3' file
"/cw-0yLGNekxk4Vup/cwdd",-112.263
"/001DLIWmCvBwVCJrQjiQA",310