简体   繁体   中英

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.

So there are multiple csv files, their format is:

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.

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. However, awk is more flexible, ie you can filter on an arbitrary column:

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

This will print the entire line is column 3 is not 0.

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

   sed  '/,0$/d' 

you can also use 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.

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

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

egrep -v ",0$" example.csv 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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