简体   繁体   English

从csv文件bash,sed,bash中删除一行

[英]Remove a line from a csv file bash, sed, bash

I'm looking for a way to remove lines within multiple csv files, in bash using sed, awk or anything appropriate where the file ends in 0. 我正在寻找一种方法来删除多个csv文件中的行,在bash中使用sed,awk或任何适当的文件以0结尾。

So there are multiple csv files, their format is: 所以有多个csv文件,它们的格式是:

EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLElong,60,0
EXAMPLEcon,120,6
EXAMPLEdev,60,0
EXAMPLErandom,30,6

So the file will be amended to: 因此该文件将被修改为:

EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLEcon,120,6
EXAMPLErandom,30,6

A problem which I can see arising is distinguishing between double digits that end in zero and 0 itself. 我能看到的一个问题是区分以零结尾的双位数和0本身。

So any ideas? 那么任何想法?

Using your file, something like this? 使用你的文件,这样的?

$ sed '/,0$/d' test.txt 
EXAMPLEfoo,60,6 
EXAMPLEbar,30,10 
EXAMPLEcon,120,6 
EXAMPLErandom,30,6

For this particular problem, sed is perfect, as the others have pointed out. 对于这个特殊问题, sed是完美的,正如其他人所指出的那样。 However, awk is more flexible, ie you can filter on an arbitrary column: 但是, awk更灵活,即您可以在任意列上进行过滤:

awk -F, '$3!=0' test.csv

This will print the entire line is column 3 is not 0. 这将打印整行第3列不为0。

使用sed仅删除以“,0”结尾的行:

   sed  '/,0$/d' 

you can also use awk, 你也可以用awk,

$ awk -F"," '$NF!=0' file
EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLEcon,120,6
EXAMPLErandom,30,6

this just says check the last field for 0 and don't print if its found. 这只是说检查最后一个字段为0 ,如果找到它则不打印。

sed '/,[ \t]*0$/d' file

我倾向于sed,但也有一个egrep(或:grep -e)-solution:

egrep -v ",0$" example.csv 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM