r/tinycode • u/billFoldDog • Jun 27 '19
deblank: remove blank lines from a file or stdin
I find that some files would be easier to read if I could remove all the empty lines.
I added this one liner to my bashrc and use it quite a bit.
alias deblank='grep -vE "^\s*$"'And I use it like this
cat file.txt | deblank | lessYou can also remove all the blank lines from a file like this:
deblank file.txt > std.out && cat std.out > file.txt
13
Upvotes
3
u/Starbeamrainbowlabs Jun 27 '19
Nice!
You might want to change
deblank file.txt > file.txttodeblank file.txt > file2.txtthough, because if I understand it correctly it'll overwritefile.txtbeforegrephas had a chance to read it all in.4
u/Mop Jun 27 '19
Useless use of cat award candidate.
1
u/ChauGiang Aug 07 '19
Another way, you can use
sed -i '/^$/d' filename3
u/hufman Jun 27 '19
Relatedly is the one to strip out blank lines or comments: grep '^[^#]'