Computing.Net > Forums > Unix > Comparing two fields from two files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Comparing two fields from two files

Reply to Message Icon

Name: hunter85
Date: May 26, 2009 at 02:38:08 Pacific
OS: Unix
Subcategory: General
Comment:

Hi Gurus. Can anyone help me with the below requirement? I need Unix shell script for that.

I am devastated, if you help me I'm really grateful to you.

Table 1:

Admin 10 20
Sports 20 19
Library 12 3

Table 2:

Admin 25 26
Dean 12 13
Library 11 22

I need output as in table3??


Table 3:

Admin 10 20 25 26
Dean NA NA 12 13
Library 12 3 11 22
Sports 20 19 NA NA

If any questions plz ask me. I really appreciate you for considering this.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 26, 2009 at 06:44:20 Pacific
Reply:

this is very common task and the tools to use include join, paste and awk. man them for more info.


0

Response Number 2
Name: hunter85
Date: May 26, 2009 at 11:16:34 Pacific
Reply:

Thanks for ur response. I actually tried with "awk". But I never
be on track and I dint know how to put "NA".

Can anyone helpme?

A will always finds a way.


0

Response Number 3
Name: ghostdog
Date: May 26, 2009 at 19:54:28 Pacific
Reply:

if you have Python,

#!/usr/bin/env python
data=open("file1").read().split("\n")
d={}
for item in data:
    i=item.split()
    d[i[0]]=i[1:3]
for line in open("file2"):
    oline= line.strip()
    oline=oline.split()
    if d.has_key(oline[0]):
        print ' '.join(oline),' '.join(d[oline[0]])
        del d[oline[0]]    
    else:
        print ' '.join(oline[0:3]),"NA NA " 
for i,j in d.iteritems():
    print i, ' '.join(j),"NA NA"

output

# ./test.py
Admin 25 26 10 20
Dean 12 13 NA NA
Library 11 22 12 3
Sports 20 19 NA NA



0

Response Number 4
Name: hunter85
Date: May 29, 2009 at 01:04:07 Pacific
Reply:

Hi Friend,

I will be grateful to your response.

Here the required solution is different from the answer given here. See the "NA" in 2nd & 4th row.

Moreover, I tried with "awk" alot. But didnt get the required output yet. I will appreciate if your response is in unix shell script.

Thankyou very much for considering my situation.

A will always finds a way.


0

Response Number 5
Name: nails
Date: May 31, 2009 at 00:20:49 Pacific
Reply:

awk version is nawk since I'm using Solaris:

#!/bin/ksh

nawk ' BEGIN {
   while ( getline < "admin1.txt" > 0 )
      myarr[$1]=$2" "$3
}
{

if($1 in myarr)
   {
   printf("%s %s %s %s\n", $1, myarr[$1], $2, $3)
   # delete the array element after printing
   delete myarr[$1]
   }
else
   printf("%s NA NA %s %s\n", $1, $2, $3)
 }

END {
# print any array elments that were not matched
for (i in myarr)
   printf("%s %s NA NA\n", i, myarr[i])

} ' admin2.txt


0

Related Posts

See More



Response Number 6
Name: hunter85
Date: June 5, 2009 at 05:11:19 Pacific
Reply:

Hi,

I found out, by using "join" we can easily solve this.

see "man join" helps a lot.

Ghostdog & Nails... My special Thanks to you. Your
suggestions helped me lot.

A will always finds a way.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Comparing two fields from two files

compare two files field string www.computing.net/answers/unix/compare-two-files-field-string/8078.html

Compare Fields in two Files www.computing.net/answers/unix/compare-fields-in-two-files/7153.html

Find un-match in two files www.computing.net/answers/unix/find-unmatch-in-two-files-/8105.html