Skip to content

Instantly share code, notes, and snippets.

@b4n
Created January 28, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4n/a15e6fc6cdaeb89c9657225afd481b04 to your computer and use it in GitHub Desktop.
Save b4n/a15e6fc6cdaeb89c9657225afd481b04 to your computer and use it in GitHub Desktop.
Script to diff configuration files (INI-style)
#!/bin/bash
# Diffs 2 configuration files, sorting their content by sections to and key
# names to get a meaningful diff.
sed_escape() {
sed 's/[][/\\.$^]/\\\0/g' <<<"$1"
}
read_section_sorted() {
local file="$1"
local section="$2"
echo
echo "$section"
sed "$file" -n -e "
/^$(sed_escape "$section")/{
:loop
n
/^\[/q
/^ *$/!p
b loop
}
" | sort -s
}
read_conffile_sorted() {
grep -e '^\[' "$1" | sort -u | while read section; do
read_section_sorted "$1" "$section"
done
}
diff -u <(read_conffile_sorted "$1") <(read_conffile_sorted "$2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment