Skip to content

Instantly share code, notes, and snippets.

@basicfeatures
Created January 5, 2023 13:40
Show Gist options
  • Save basicfeatures/c5b1f8952aafcef865977e383985ac58 to your computer and use it in GitHub Desktop.
Save basicfeatures/c5b1f8952aafcef865977e383985ac58 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# CLEANS UP TEXT FILES
setopt nullglob
setopt extendedglob
# Skip vendors
for file in (^vendor/)#*(.); do
# Match text-files
if file -b $file | grep -q "text"; then
# Read file into a variable
raw=$(<$file)
# Remove CR+LF (^M)
raw=${raw//$'\r'}
# Remove trailing whitespace
raw=${raw//$'[ \t]##\n'/$'\n'}
# Compress two or more consecutive blank lines
raw=${raw//$'\n'(#c3,)/$'\n\n'}
# Save result while ensuring newline at EOF
printf "%s\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