Vcode
Vcode

Reputation: 195

Compare two files in unix and add the delta to one file

Both files has lines of string and numeric data minimum of 2000 lines. How to add non duplicate data from file2.txt to file1.txt. Basically file2 has the new data lines but we also want to ensure we are not adding duplicate lines to file1.txt.

thanks,

Upvotes: 2

Views: 3012

Answers (4)

hek2mgl
hek2mgl

Reputation: 157967

You can use grep, like this:

# grep those lines from file2 which are not in file1
grep -vFf file1 file2 > new_file2
# append the results to file1
cat new_file2 >> file1

Upvotes: 1

gregory
gregory

Reputation: 12895

use awk:

awk '!a[$0]++' File1.txt File2.txt

Upvotes: 1

Wrikken
Wrikken

Reputation: 70460

Another option if the file is sorted, just to have some choice (and I like comm :) )

comm --check-order --output-delimiter=''  -13 File1.txt File2.txt >> File1.txt

Upvotes: 2

Barmar
Barmar

Reputation: 780974

Sort the two files together with the -u option to remove duplicates.

sort -u File1.txt File2.txt > NewFile.txt && mv NewFile.txt File1.txt

Upvotes: 4

Related Questions