Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: One-liner removes whitespace-only lines from text file

by FoxtrotUniform (Prior)
on Dec 19, 2001 at 02:21 UTC ( #132956=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://132956]
help
Chatterbox?
[shmem]: hmm after some looping I get it via http
[choroba]: After submitting the form, I'm getting "Sorry, we're really busy right now, please wait for a bit. This page will automagically refresh soon"
[hippo]: Still nothing via https
marto pictures someone trying to fix a problem, while the page gets battered by F5s :P
[hippo]: Who maintains deps? Is it barbie?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2018-02-20 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (271 votes). Check out past polls.

    Notices?