Wednesday, May 23, 2012

Join Two files in sort --- we can use Join

I have urgent requirment that I need to join two files on one of the key. Please find below example and please any one help me in this case. 
Bufello 22222 
Chiktowagha 11111 
New York 33333 
Chiktowagha 14227 
Bufello 14203 
Now you can see that there are 3 records in file1 and 2 records in file 2 , so I need output as below 
Bufello 14227 
Chiktowagha 14203 
New York 00000 
that mens , it should pick up first filed from file 1 and corresponding second fild from file 2 but if any key from file1 is not found in file 2 the it should write 0 Zero in second field of that value 
I tried it by JOINKEYS , but I could not get it done .................. 
Can any one help me to get it done by sort ( any UTILITy in Sort) 
Please its Urgent for me !!!!!

while read line; do 
        col1=$(echo $line |\ 
        awk 'BEGIN {s=""} { 
        n=split($0,a," "); 
        if (n>2) { 
                for (i=1; i<=(n-1); i++) { 
                        if (length(s)) s=s" "a[i] 
                        else s=a[i] 
                print s; 
        } else print $1; 
        n=$(grep "$col1" $f2) 
        (($?==0)) && print $n || print "$col1 0" 
done <$f1


Post a Comment

Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM