Remove the line from the csv file bash, sed, bash

I am looking for a way to delete lines in multiple csv files, in bash using sed, awk or something suitable where the file ends with 0.

So, there are several csv files, their format:

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

Thus, the file will be changed to:

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

The problem I see is the difference between two-digit numbers that end in zero and 0.

So any ideas?

+3
source share
6 answers

Using your file, something like this?

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

For this particular task is sedperfect, as others have pointed out. However, it awkis more flexible, i.e. You can filter an arbitrary column:

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

, 3 0.

+5

sed , ", 0":

   sed  '/,0$/d' 
+2
source

you can also use awk,

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

it just says check the last field for 0and do not print if it is found.

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

I would lean towards sed, but there is also an egrep (or: grep -e) solution:

egrep -v ",0$" example.csv 
+2
source

Source: https://habr.com/ru/post/1741281/


All Articles