Getting a text file rid of all superfluous blank lines

Les Mikesell lesmikesell at gmail.com
Wed Nov 30 17:40:01 UTC 2005


On Wed, 2005-11-30 at 06:42, Paul Smith wrote:
> >
> > cat --squeeze-blank inputfilename -> outputfilename
> 
> Thanks, Tim and Paul. Paul's method does not mysteriously work:
> 
> $ more file1.txt
> word1
> 
> 
> 
> word2
> 
> word3
> $ more -s file1.txt > file2.txt
> $ more file2.txt
> word1
> 
> 
> 
> word2
> 
> word3
> $
> 
> Tim's way works partially, i.e., many blank lines are in effect
> erased, but some remain. I suspect that the left blank lines are not
> blank lines although they look like blank lines. Can one go further
> with deleting the left "false" blank lines?

In vi:
:%s/^[ 	]*$//
That says for the range of all lines, substitute any number of white
spaces (there's a space and tab inside the []'s) filling from the
beginning (^) and end ($) of the line with nothing.
If you don't like the results, hit 'u' (undo).
then
1G!Gcat -s
which says filter the range from the first through last line through
the command cat -s and replace the buffer with the results.
Again, if you don't like the results, hit 'u'.  Repeat until
you get it right.

-- 
  Les Mikesell
    lesmikesell at gmail.com





More information about the users mailing list