Thursday, May 5, 2016

Merging multiple files using first column as hash keys with awk - part 3

Modification of the script from the previous post, where rows with missing values are not excluded but rather missing values are set to 0.

NR==FNR { h1[$1] = $1; h2[$1] = $2; next; }
NR!=FNR { l=FNR-1;}
NF{ if($1 in h1) h2[$1] = h2[$1] OFS $2;}
NF{ if($1 in h1 && $2=="") h2[$1] = h2[$1] OFS 0}
END { for(k in h2)
  if(split(h2[k], a) > ARGC-2)
    print k OFS h2[k]
}
Example, 4th row in 4.test is missing a value in col2.

mail 5
now 7
tomorrow 7
string 5
do 6
DN52ei1f:~ milospjanic$ cat 2.test 
mail 4
now 4
tomorrow 4
string 4
do 4
DN52ei1f:~ milospjanic$ cat 3.test 
mail 6
now 6
tomorrow 7
string 5
do 67
DN52ei1f:~ milospjanic$ cat 4.test 
mail 89
now 75
tomorrow 75
string 
do 555
DN52ei1f:~ milospjanic$ awk -f script.awk 1.test 2.test 3.test 4.test 
tomorrow 7 4 7 75
do 6 4 67 555
now 7 4 6 75
string 5 4 5  0
mail 5 4 6 89

No comments:

Post a Comment