Skip to content

Instantly share code, notes, and snippets.

@flxai
Last active December 31, 2015 09:27
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 flxai/c6ec6c0c5f2e3b864ddd to your computer and use it in GitHub Desktop.
Save flxai/c6ec6c0c5f2e3b864ddd to your computer and use it in GitHub Desktop.
Fixes some ugly fastd peers' keys
grn='\e[0;32m'
red='\e[0;31m'
clr='\e[0m'
for i in $1/*; do
fn=$(basename $i)
# comment keys
grep -qc '^# \?key' $i
ckeyworks=$?
[[ $ckeyworks = 1 ]] && ckeycol=$grn || ckeycol=$red
# date
grep -qc '^# [0-9]\{4\}\.' $i
dateworks=$?
[[ $dateworks = 1 ]] && datecol=$grn || datecol=$red
# emptylines
grep -qc '^$' $i
elworks=$?
[[ $elworks = 1 ]] && elcol=$grn || elcol=$red
# newlines
nlworks=$(tail -1 $i | wc -l)
[[ $nlworks = 1 ]] && nlcol=$grn || nlcol=$red
# trailing whitespace
grep -qc ' $' $i
twworks=$?
[[ $twworks = 1 ]] && twcol=$grn || twcol=$red
# print info
echo -e "$(printf '%40s' "$fn"): ${ckeycol}key$clr ${datecol}date$clr ${elcol}emptyline$clr ${nlcol}newline$clr ${twcol}whitespace$clr"
# fix comment keys
[[ $ckeyworks = 0 ]] && sed -i "/^#\bkey\b/d" $i
# fix date
[[ $dateworks = 0 ]] && sed -i 's/^# \([0-9]\{4\}\)\.\([0-9]\{2\}\)\.\([0-9]\{2\}\)/# \1-\2-\3/' $i
# fix emptylines
[[ $elworks = 0 ]] && sed -i '/^$/d' $i
# fix newlines
[[ $nlworks = 0 ]] && sed -i -e '$a\' $i
# fix trailing whitespace
[[ $twworks = 0 ]] && sed -i 's/[[:space:]]*$//' $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment