Saiyam Vora
Saiyam Vora

Reputation: 11

Remove rows based on values in a column

I would like to remove all rows in my CSV where under the column "Owner" The Value is Fishbowl Digital Content.

Here is a sample CSV

ID,Name,Function,Media,Owner
415,Sam,Run,Footage,Production
213,Raj,Catch,Footage,Fishbowl Digital Content
214,Jack,Hold,Website,Salvage
256,Jason,Catch,Website,Fishbowl Digital Content

I have tried

awk -F , '$4 != "Fishbowl Digital Content" {print $0}' Test.csv >TestModified.csv

But I still see lines where the Value under "Owner" is 'Fishbowl Digital Content'

awk -F , '$4 != "Fishbowl Digital Content" {print $0}' Test.csv >TestModified.csv

Here are the desired Results:

ID,Name,Function,Media,Owner
415,Sam,Run,Footage,Production
214,Jack,Hold,Website,Salvage

Upvotes: 1

Views: 239

Answers (3)

aborruso
aborruso

Reputation: 5728

I know it's not awk, but I find that the Miller way (http://johnkerl.org/miller/doc/) is very easy and useful

mlr --csv filter -x '$Owner=="Fishbowl Digital Content"' inputFile.csv

Upvotes: 1

codeforester
codeforester

Reputation: 43079

You can also use grep in case you are matching the first or the last column. For the last column, here is the filter:

grep -v ',"Fishbowl Digital Content"$' file

If there could be trailing spaces, then:

grep -vE ',"Fishbowl Digital Content"[[:space:]]*$' file

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133750

1st Solution(With hard-coding field value): Could you please try following.

awk -F, 'FNR==1{print;next} $NF!="Fishbowl Digital Content"' Input_file

2nd solution(Generic solution without hard-coding field value): Adding more generic solution in which it will check which field is having Owner value and then it will skipm lines. Meaning we are NOT hard coding value of Owner column here.

awk -F, 'FNR==1{for(i=1;i<=NF;i++){if($i=="Owner"){val=i}};print;next}  $val!="Fishbowl Digital Content"' Input_file

Upvotes: 0

Related Questions