Created
April 13, 2020 19:52
-
-
Save anon987654321/2037b7942d5c0784d6f4b3f9c11df5ef to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env zsh | |
# | |
# CLEANS UP TEXT FILES | |
# | |
# Dependency check | |
function die { | |
print >&2 "$@" | |
exit 1 | |
} | |
for command in file; do | |
(( ${+commands[$command]} )) || failed+=($command) | |
done | |
if (( $#failed )); then | |
die "Not found: $failed" | |
fi | |
setopt nullglob | |
setopt extendedglob | |
for file in **/*; do | |
# Match text files | |
if file -b $file | grep -q "text"; then | |
# Read file into a variable | |
raw=$(<$file) | |
# Remove CR LF | |
raw=${raw//$'\r'} | |
# Remove trailing whitespace | |
raw=${raw//[[:blank:]]##$'\n'/$'\n'} | |
# Replace tabs with whitespace | |
raw=${raw//$'\t'/ } | |
# Compress two or more consecutive blank lines | |
raw=${raw//$'\n'(#c3,)/$'\n\n'} | |
# Write the variable back into the file | |
printf "%s\n\n" $raw > $file | |
echo $file | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment