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
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.