Skip to content

Instantly share code, notes, and snippets.

@leahcim
Last active August 29, 2015 14:02
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 leahcim/4805b0168cfcfe3aec88 to your computer and use it in GitHub Desktop.
Save leahcim/4805b0168cfcfe3aec88 to your computer and use it in GitHub Desktop.
A bash script for trimming extra blank lines. Consecutive blank lines are collapsed into one. Multiple files can be given as arguments at once.
#!/bin/bash
for file in "$@"; do
TEMP=temp.$RANDOM
sed "s/\s*$//" "$file" | # Trim whitespace from ends of lines
cat -s | # Collapse consecutive blank lines into one
tr "\n" "\0" | # Replace new line characters with nulls
sed "s/\x0*$//" | # Trim nulls from the end
tr "\0" "\n" > "$TEMP" # Replace nulls with new line characters
mv -f "$TEMP" "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment