Home > Enterprise >  Delete rows of a CSV (file) with awk
Delete rows of a CSV (file) with awk

Time:12-07

Seawater_dat.csv:

submission ID, NAME, COUNT, Location
S34575265, Snow Goose, 12, Pt. Pinos
S34575294, Snow Goose, X, Pt. Pinos
S34575294, Snow Goose, 1, Pt. Pinos
S34575294, Snow Goose, 1, Pt. Pinos
S34575294, Snow Goose, X, Pt. Pinos

What I need to do is remove every row that contains X value associated with count through awk.

Code that I tried:

$ awk -F ‘$3X’ Seawatch_dat.csv

It resulted in error stating unexpected character '.' I am assuming my command prompt is incorrect. What I expected to happen was that every row containing X would be deleted by running code.

Expected Result:
Seawater_dat.csv:

submission ID, NAME, COUNT, Location
S34575265, Snow Goose, 12, Pt. Pinos
S34575294, Snow Goose, 1, Pt. Pinos
S34575294, Snow Goose, 1, Pt. Pinos

CodePudding user response:

Would you please try:

awk -F, '$3 !~ /X/' Seawater_dat.csv

As a side note, the file is not a csv file in a strict sense as it includes unnecessary whitespaces after commas.

CodePudding user response:

According to the sample data in your question, you want to keep only those lines from the CSV file that do not contain a capital X.

awk '!/X/ {print}' Seawater_dat.csv

Refer to this JDoodle
(Note that you have to copy the contents of file Seawater_dat.csv to the Stdin Inputs field before clicking the Execute button.)

  • Related