Created
December 19, 2012 08:29
-
-
Save ooharak/4335292 to your computer and use it in GitHub Desktop.
Merge entries to a line-oriented key-value configuration file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## | |
## by ooharak 2012. | |
## | |
## _merge_entries target_file < merge_file | |
## update entries in target_file with merge_file. | |
function _merge_entries { | |
local file=$1 | |
local entry | |
while read entry ; do | |
local head=`echo "$entry"|cut -b 1` | |
if [ "$head" == "#" ]; then | |
local key=`echo "$entry" | cut -b 2- | cut -d = -f 1` | |
sed -i -e "s/^$key/#$key/g" $file | |
else | |
local key=`echo "$entry" | cut -d = -f 1` | |
sed -i -e "s/^$key/#$key/g;\$a$entry" $file | |
fi | |
done | |
} | |
function _test { | |
cat <<-EOF > test.txt | |
salary=100 | |
empno=123 | |
name=john | |
EOF | |
_merge_entries test.txt <<-EOF | |
salary=200 | |
#empno= | |
deptno=456 | |
EOF | |
cat <<-EOF > expected.txt | |
#salary=100 | |
#empno=123 | |
name=john | |
salary=200 | |
deptno=456 | |
EOF | |
diff expected.txt test.txt && echo "TEST OK" || echo "TEST NG" | |
rm -f test.txt expected.txt | |
} | |
_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment