python or awk comparing files
Question: I have two files that are tab delimited.I need to compare file 1 column 3 to
file 2 column 1 .If there is a match I need to write column 2 of file 2 next
to the matching line in file 1.here is a sample of my file:

file 1:

    
    
    a rao rocky1 beta
    
    b rao buzzy2 beta
    
    c Rachel rocky2 alpha
    

file 2:

    
    
    rocky1 highlightpath
    
    rimper2 darkenpath
    
    rocky2 greenpath
    

output:

new file:

    
    
    a rao rocky1 beta highlightpath
    
    b rao buzzy2 beta
    
    c Rachel rocky2 alpha greenpath
    

the problem is file 1 is huge ! file 2 is also big but not as much. So far I
tried awk command , it worked partially. what I mean is number of lines in
file 1 and output file which is newfile should be same, which is not what I
got ! I get a difference of 20 lines.

    
    
    awk 'FNR==NR{a[$3]=$0;next}{if($1 in a){p=$1;$1="";print a[p],$0}}' file1 file2 > newfile
    

So I thought I could try python to do it, but I am a novice at python. All I
know so far is I would like to make a dictionary for file 1 and file 2 and
compare. I know how to read a file into dictionary and then I am blank.Any
help and suggestion with the code will help. Thanks


Answer: 
    import sys
    
    # Usage: python SCRIPT.py FILE1 FILE2 > OUTPUT
    file1, file2 = sys.argv[1:3]
    
    # Store info from the smaller file in a dict.
    d = {}    
    with open(file2) as fh:
        for line in fh:
            k, v = line.split()
            d[k] = v
    
    # Process the bigger file line-by-line, printing to standard output.
    with open(file1) as fh:
        for line in fh:
            line = line.rstrip()
            k = line.split()[2]
            if k in d:
                print line, d[k]
            else:
                print line
    

