Skip to content

Instantly share code, notes, and snippets.

@anon987654321
Created April 13, 2020 19:52
Show Gist options
  • Save anon987654321/2037b7942d5c0784d6f4b3f9c11df5ef to your computer and use it in GitHub Desktop.
Save anon987654321/2037b7942d5c0784d6f4b3f9c11df5ef to your computer and use it in GitHub Desktop.
#!/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