简体   繁体   中英

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.

You have to correct your like below

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

您可以使用grep获得预期的输出:

grep -vf file2 file1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM