user3685928
user3685928

Reputation: 103

Remove lines from file if the first column appears in another file

I have to files like this:

File1:

id1 junk junk junk 

id2 junk junk junk 

id3 junk junk junk 

id4 junk junk junk

File2:

id2

id3

How can I remove the 2 lines from File1 that has id in File2. So the output would be

id1 junk junk junk

id4 junk junk junk

Thank you

Upvotes: 2

Views: 49

Answers (2)

Walter A
Walter A

Reputation: 19982

You can tell grep to exclude everything from file2

grep -vf file2 file1

Upvotes: 2

anubhava
anubhava

Reputation: 784998

Using awk you can do this:

awk 'FNR==NR{a[$1]; next} !($1 in a)' file2 file1

id1 junk junk junk
id4 junk junk junk

Or using grep -v with process substitution:

grep -vf <(sed 's/.*/^&[[:blank:]]/' file2) file1

id1 junk junk junk
id4 junk junk junk

Upvotes: 2

Related Questions