Skip to content

Instantly share code, notes, and snippets.

@n-st
Created March 24, 2016 21:51
Show Gist options
  • Save n-st/cf01e0f92d4ec09c751a to your computer and use it in GitHub Desktop.
Save n-st/cf01e0f92d4ec09c751a to your computer and use it in GitHub Desktop.
Sort the sections of a Git config file (and the contents of the sections) alphabetically
awk '{ if ($1 ~ /^\[/) { section=$0; print $0 } else { printf section; print $0 } }' gitconfig | sort -t ']' -k1,1 | sed 's/\[.\+\]\([^\s]\+\)/\1/' > gitconfig.new
mv gitconfig.new gitconfig
@CervEdin
Copy link

CervEdin commented Dec 6, 2022

Nice! 🚀

But it fails for keys containing brackets.

Something like this avoids that

awk '{ if ($1 ~ /^\[/) { section=$0; printf "%s\t\n", $0 } else { printf section; print $0 } }' .gitconfig |
  sort -t ']' -k1,1 |
  sed '/\t$/{s@@@;p;d};s@^[^\t]*@@' |
  sponge.sh .gitconfig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment