here2learn
here2learn

Reputation: 61

Compare Two Files and Print Lines That Don't Match

I'm trying to compare two files (file1 and file2) and print the full lines from file1 that don't match the list from file2 - ideally in a new .txt file, however when I run awk it's not printing anything.

file1 example                    file2 example
12345 /users/test/Desktop        543252 
54321 /users/test/Downloads      12345  
0000  /users/test/Desktop        11111
                                 0000

expected output
54321 /users/test/Downloads 

The command I've tried is

awk 'NR==FNR{a[$1]++;next};a[$1] ==0' file1.txt file2.txt

ideally I'd like to be able to build this into a python program I'm writing (don't know if that's possible) if not I'd be happy for it to run through the linux terminal.

Any thoughts or pointers would be gratefully received.

Upvotes: 1

Views: 5293

Answers (2)

Akshay Hegde
Akshay Hegde

Reputation: 16997

You have to correct your like below

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

Upvotes: 4

Zumo de Vidrio
Zumo de Vidrio

Reputation: 2091

You can get the expected output with grep:

grep -vf file2 file1

Upvotes: 3

Related Questions