Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all lines from a CSV file containing an exact match of a search term?

Tags:

bash

sed

awk

I have a CSV file like this:

text,0
more text,2
some more text,100

I need to delete any line containing only 0 in the second column, e.g., the output of the above would be:

more text,2
some more text,100

How can I delete all lines from a CSV with an exact match?

like image 660
Village Avatar asked Dec 17 '22 01:12

Village


2 Answers

If that's your last field, grep will do the trick:

grep -v ',0$'

If not, and your fields don't contain ,, use awk:

awk -F , '{if ($2!='0') print}'

If it's even more complex use python or ruby with a CSV parser.

like image 120
Karoly Horvath Avatar answered May 13 '23 02:05

Karoly Horvath


A simple sed solution...

sed /,0$/d
like image 32
Peter.O Avatar answered May 13 '23 03:05

Peter.O