Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read two files, if duplicate found ignore, otherwise print 2

Status
Not open for further replies.

hsemple

Programmer
Feb 25, 2005
3
CA
Hi,

I have 2 data files that contain 2 days worth of transactions. For example File1 contains Mon & Tues data and File2 contains Tues & Wed data. There will be some additional Tues records in File2 that were not in File1 due to the time that the file was created.

I need to be able to read the two files and compare File1 with File2. I need to print (to an output file)only the records in File2 that do not appear in File1.
 
If you wanna compare whole lines and not just some parts, that will do for totals: awk '! Seen[$0]++' file1 file2
Basically, awk 'BEGIN { while (getline <"file1") Seen[$0]=1 } ! Seen[$0]' file2

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Lines are printed if
1. They are in the 2nd file; and
2. they weren't in the first file.
Code:
NR==FNR { a[$0];next }  # First file.
!($0 in a)              # Second file.
 
futurelet,

Thanks for the response, however, when I tried your code it didn't work out as I had expected.

I added the code to my unix script but the output file that gets created is the same as the $histfile, it doesn't pick out the unique records:

# First line reads first file.
# Second line compares to second file.
# If the record is in the second file but not in the first file then the record is printed.
awk '
NR==FNR
{a[$0];next}
!($0 in a)
' $histfile $datafile > $datafile.bak

 
awk '
NR==FNR{a[$0];next}
!($0 in a)
' $histfile $datafile > $datafile.bak

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Not
[tt]
NR==FNR
{a[$0];next}
[/tt]
but
[tt]
NR==FNR {a[$0];next}
[/tt]
You changed the code.
 
Thanks to Futurelet and PHV for setting me straight on that one line of code that I had messed up by splitting it up! It works fine now.
 
that's a rare site to have both of you guys thanked in the same sentence [wink]

congrats!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top