Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

(code) One-liner removes whitespace-only lines from text file

by ybiC (Prior)
on Dec 19, 2001 at 02:15 UTC ( #132953=snippet: print w/replies, xml ) Need Help??
Description: Perl one-liner to remove from a text file all lines that contain only whitespace.

Other Ways To Do It (mostly not one-liners) can be found at the thread "How do I remove blank lines from text files?".

Hmmm... personally, I like Ovid's example below better than mine.   It *does* seem more directly recognizable.



perl -i.bak -ne 'print unless /^\s+$/' file.txt

Replies are listed 'Best First'.
(Ovid) Re: One-liner removes whitespace-only lines from text file
by Ovid (Cardinal) on Dec 19, 2001 at 02:32 UTC
Re: One-liner removes whitespace-only lines from text file
by FoxtrotUniform (Prior) on Dec 19, 2001 at 02:21 UTC

    This snippet removes all non-empty lines containing only whitespace, but I don't think that's particularly intuitive behaviour. If I want to remove all "blank" lines from a file, I probably want:

    perl -i.bak -ne 'print if /\S/' file.txt

    and if I want to get rid of all the lines with "useless" whitespace, I probably want to collapse them to empty lines:

    perl -i.bak -ne 's/^\s+$//;print' file.txt

    Update: Changed first one-liner to Ovid's for readability.

    --
    :wq
Re: One-liner removes whitespace-only lines from text file
by jmcnamara (Monsignor) on Dec 19, 2001 at 14:00 UTC

    This is attributable to abigail:      perl -lp00ee file

    Update: As chip points out, this one-liner removes blank lines and not whitespace-only lines.

    --
    John.

      Well, 10 out of 10 for cleverness, but abigail's trick only removes empty lines, i.e. those that match /^$/; but the original requirement was to remove lines made up of any combination of whitespace.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://132953]
help
Chatterbox?
[ambrus]: erix: that one actually sucks. these days people should get rid of the old notion that TeX is the only thing you can use for decent mathematics writing, because MS Office and LibreOffice have reached the
[ambrus]: level where people can more easily write as good mathematical papers in them as the people who write bad LaTeX papers usually write.
[ambrus]: Yes, for like the first twenty of its years, TeX was basically the only system that allowed you to write decent maths papers, and C++ and PHP were programming languages that sucked, etc. But times change and people have to accept that.
Discipulus bad people + good tool < normal people + decent tool
[Discipulus]: php does not suck anymore?
[ambrus]: Discipulus: I'm not sure, but it certainly doesn't suck as much as it's used to. it's like C++, it sucks because people still recursively learn from twenty year old PHP examples,
[ambrus]: and they try to use the obsolete features that PHP has to support only for compatibility with old scripts. C++ and PHP both have the problem that people can't forget the past, because when they google "PHP" plus the problme they want to solve, they find b
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-09-26 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?