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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare two files 2

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
CO
Hi, [thumbsup2]

I have two files that I wish to compare them. These files have the first and second field in common. I wish to obtain a new file only with the registers of the second file.

I did a program in perl but it is delayed long time, I don`t know why. Is possible to do that with the join command??

Is necesary to order these files before to compare them??

file1.txt
03102088520|047823026|0411171635410411171644580009200093BIANUAL1|P00000614730211110011BIAC7I|BIAC7O|00000D000|0000000000917
012859206|047730485|0411171640360411171645230004700049BIANUAL1|P00000323890211110011BIAC7I|BIAC7O|00000D000|0000000000447
012590129|047724078|0411171658290411171707260008900090BIANUAL1|P00000594900211110011BIAC7I|BIAC7O|00000D000|0000000000857
012405772|047724198|0411171908540411171910160001300016BIANUAL1|P00000105760211110011BIAC7I|BIAC7O|00000D000|0000000000122

....
.
.

file2.txt

03102088520|047823026|0411171555130411171555340000300010MONTDMS1|P000000280002111270110070|0019|00000A000|0000000000021
03102407085|047811617|0411171507320411171507420000100010MONTDMS1|P000000280002111270110070|0032|00000A000|0000000000010
03102845756|047833419|0411171542570411171544170001300016MONTDMS1|P000000448002111270110070|0017|00000A000|0000000000120
03103019838|048940045|0411171500130411171504540004600049MONTDMS1|P000001372002111270110070|0058|00000A000|0000000000441
03103220823|047833215|0411171545100411171549560004700049MONTDMS1|P000001372002111270110070|0017|00000A000|0000000000446
03103234521|047827998|0411171549340411171551420002100023MONTDMS1|P000000644002111270110070|0019|00000A000|0000000000208

Malpa
Thanks for your help.

 
only with the registers of the second file.
Can you please post expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PVH Thanks for your assistant.


That`s correct. In the example of above only I want the registries or lines of the second file (file2.txt) that agree or match with the first and second fields of the first file (file1.txt).

The amount of registers or lines of each one files are different.

Result

03102088520|047823026|0411171555130411171555340000300010MONTDMS1|P000000280002111270110070|0019|00000A000|0000000000021

Malpa

Thanks again.
 
Unfortunately the join command admits only one joining field.
You may try something like this:
awk -F'|' '
FNR==NR{t[$1]=$2;next}
t[$1]==$2
' file1.txt file2.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PVH.

Could you explain me how does it works??
before to run this program Is necesary to sort these files??
If I want to print the registers of the first file that doesn't match with the second file How I modify it ??


awk -F'|' '
FNR==NR{t[$1]=$2;next}
t[$1]==$2
' file1.txt file2.txt > match



Malpa
Thanks again.
 
Could you explain me how does it works
man awk
Is necesary to sort these files
No
print the registers of the first file that doesn't match with the second file
awk -F'|' '
FNR==NR{t[$1]=$2;next}
!($1 in t) || t[$1]!=$2
' file2.txt file1.txt > nomatch

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PVH Thanks for your assistant.
It was helpful[thumbsup2].
You deserve a star
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top