-
-
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.
This file contains hidden or 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
#!/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