Search code examples
fileubuntuduplicatesunique

ubuntu, extract duplicate value from 2 files


how to extract duplicate and unique values from 2 files in ubuntu and save them in separate files for example

file1.txt

abc
123
321

file2.txt

abc
123
321
456

how to extract duplicate and uniques?

output for duplicates between 2 files

duplicates.txt

abc
123
321

output for unique values between 2 files

unique.txt

456

I tried this awk 'NR==FNR{a[$1];next}$1 in a' file1.txt RS="" file2.txt

but did not get only duplicate and uniques, but I got all values


Solution

  • ok I have found solutions

    to get duplicates

    sort *.txt | awk '{print $1}' | uniq -d
    

    to get unique, not correspondiong values

    awk 'FNR==NR {a[$0]++; next} !($0 in a)' file1.txt file2.txt
    or 
    sort *.txt | awk '{print $1}' | uniq -u