Compairson and merging of files.

Sorry for the duplicate post as there seem to be hundreds of similar posts - I have searched - however due to my limited scripting knowledge I can not seem to modify any of the other answers to suit my needs.

I have two files:

ONE: Each value, h1, h2 etc... only appears once.

h1 0000 0000
h2 0000 0000
c 0000 0000
ca 0000 0000
c1 0000 0000

TWO: Values h1... can appear more than once or may not appear at all i.e. h2

h1 1 2 3 4
c 1 2 3 4
ca 1 2 3 4
h1 1 2 3 4
c1 1 2 3 4
h1 1 2 3 4

If the first columns match the values from the first file need to be appened to the second.

i.e.

h1 1 2 3 4 0000 0000
c 1 2 3 4 0000 0000
ca 1 2 3 4 0000 0000
h1 1 2 3 4 0000 0000
c1 1 2 3 4 0000 0000
h1 1 2 3 4 0000 0000

I have attempted this with python and awk.

Any help would be greatly appreciated!:slight_smile:

awk ' FILENAME=="file1" { tmp=sprintf("%s %s", $2, $3);  arr[$1]= tmp}
        FILENAME=="file2" { printf("%s %s\n", $0, arr[$1]) }
       '  file1 file2 > newfile

Use nawk or /usr/xpg4/bin/awk on Solaris.

awk 'NR == FNR {
  _[$1] = $2 FS $3
  next
  }
$1 in _ { 
  $(NF+1) = _[$1] 
  } 
1' file1 file2

Thanks for the v.quick reply.

Both worked on one system(Ubuntu) and errors galore on another(Red Hat)?

Can this be accomplished using python? This would be preferred as it is part of a larger python script.

It should work if you have GNU awk (gawk) on RedHat.