Bash comparing two different files with different fields -


i not sure if possible want compare 2 character values 2 different files. if match want print out field value in slot 2 1 of files. here example

# file 1  date d tamb b  # file 2 f gge0001x gge0001y gge0001z d 12-30-2006 12-30-2006 12-30-2006 t 14:15:20 14:15:55 14:16:27 b 15.8 16.1 15 

here thought behind problem want do

if [ (field2) (file1) == (field1) (file2) ] ;     echo (field1 file1) , (field2 file2) on same line  prints out "date 12-30-2006"                   "tamb 15.8"                  " ... " 

and continually run through every line file 1 printing out matches there are. assuming these need sort of array involved. thoughts on if correct logic , if possible?

this reformats file2 based on abbreviations found in file1:

$ awk 'fnr==nr{a[$2]=$1;next;} $1 in {print a[$1],$2;}' file1 file2 date 12-30-2006 tamb 15.8 

how works

  • fnr==nr{a[$2]=$1;next;}

    this reads each line of file1 , saves information in array a.

    in more detail, nr number of lines have been read in far , fnr number of lines have been read in far current file. so, when nr==fnr, know awk still processing first file. thus, array assignment, a[$2]=$1 performed first file. statement next tells awk skip rest of code , jump next line.

  • $1 in {print a[$1],$2;}

    because of next statement, above, know that, if line, working on file2.

    if field 1 of file2 matches field 2 of file1, print reformatted version of line.