Arpit Arpit - 2 months ago 13
Bash Question

Deleting complete record in File 1 when a string from file 2 is found

I have one file which has data:

ID Name
1 abc
2 def
....


I have another file which has the strings:

ID
1
9
10
...


I want to search the data file with strings in the string file(having just ID) and if the string is found in the data file then delete ONLY that Record.

So in the above example:

Only the record with ID=1 should get deleted from the data file.

This has to be done on a unix system.

I have tried this:

grep -v -F /path/to/string/file/string.txt /path/to/data/file/data.csv


This lists out the content but does not edit/delete the record in the data file.

Thanks

Answer

You can use this awk solution:

awk 'NR==FNR{keep[$1]; next} !($1 in keep)' string.txt data.csv
2  def

Reference: Effective AWK Programming