Use of join

I want basically to merge two config files each of them look like that

File:Option1 value1
Optionx valuex
....
One of those files is default config while the other is generated by my script. Now here is the problem, when my script generates an option that previously has been set by default i want to override it. So if we have situation like that

Default:Option1 DefaultValue1
Option2 DefaultValue2
GeneratedConfig:Option1 Value1
Option3 Value3
Option4 Value4

After joining those files I want to get

JoinedFile:Option1 Value1
Option2 DefaultValue2
Option3 Value3
Option4 Value4
I prefer bash solutions to others, but anything that works will do. Thanks a lot.

awk '{A[$1]=$0} END {for ( i in A ) print A}' default generated

ksh93:

#!/bin/ksh
typeset -A settings
cat default generated|
while read option val; do
  settings[$option]=$val
done
for i in "${!settings[@]}"; do
  echo "$i ${settings[$i]}"
done | sort

---------- Post updated at 02:03 PM ---------- Previous update was at 11:57 AM ----------

In bash it is more complicated because of the lack of associative arrays.

#!/bin/bash
set -a options
set -a vals
i=0
while read option val; do
  for (( j=0; j<i; j++ )); do
    if [[ ${options[j]} = $option ]]; then
      break
    fi
  done
  options[j]=$option
  vals[j]=$val
  if (( j==i )); then
    (( i++ ))
  fi
done < <(cat  default generated)
for (( j=0; j<i; j++ )); do
  echo "${options[j]} ${vals[j]}"
done

Thanks for the solution. But will that work when value field isn't just one word but couple of them? I can't test it right now as i'm not at my linux box.

This perl solution will work for even more than one word values.

open DFH,"<$ARGV[0]";

while(<DFH>)  {
    ($key, $value) = ( $_ =~ /(^[^ ]*)(.*)$/ );
    $default{$key} = $value;
}

open GFH,"<$ARGV[1]";

while (<GFH>)  {
    ($key, $value) = ( $_ =~ /(^[^ ]*)(.*)$/ );
    $gfh{$key} = $value;
}

$i=0;
while(++$i)  {
    if ( defined $gfh{"Option$i"} )  {
        print "Option$i $gfh{\"Option$i\"}\n";
    } elsif ( defined $default{"Option$i"} )  {
        print "Option$i $default{\"Option$i\"}\n";
    } else {
        exit(0);
    }
}

It should be executed like the following.

$ perl t.pl default generatedconfig 
Option1  Value1
Option2  Default Value2
Option3  Value3
Option4  Value4

Note: It will work for the given input, no assumption made.
Obviously it is not a very strong solution, as the most of cases are not handled.

You're welcome. Yes, all 3 solutions will also work when the value fields consist of a couple of words, both default and generated values.

S.