azCats azCats - 5 months ago 25
Linux Question

Linux - How to remove certain lines from a files based on a field value

I want to remove certain lines from a tab-delimited file and write output to a new file.

a b c 2017-09-20
a b c 2017-09-19
es fda d 2017-09-20
es fda d 2017-09-19

The 4th column is Date, basically I want to keep only lines that has 4th column as "2017-09-19" (keep line 2&4) and write to a new file. The new file should have same format as the raw file.

How to write the linux command for this example?

Note: The search criteria should be on the 4th field as I have other fields in the real data and possibly have same value as 4th field.

Answer Source

With awk:

awk 'BEGIN{OFS="\t"} $4=="2017-09-19"' file

OFS: output field separator, a space by default

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download