Need a simple PERL script to remove blank lines from txt file

Posted on 2006-05-06
Last Modified: 2006-11-18
I need a  perl script that will iterate through a txt file and REMOVE any lines that match this regex: m/\A(?:^\|.\|..\|.+$)\z/m)
An example of a line the regex is to match is: "| |  | +--ActiveX" (in case my regex isn't ideal).
The name of the txt file can be hard coded, as it will not change (don't need to prompt for it). It will be "tree.txt".
Lastly, I would rather have a few more lines of code in order for the script to be easily understood, as this is more important to me than brevity.
Thank you.
Question by:lessthan2
    LVL 4

    Expert Comment


    you probably want to escape the +

    perl -ne "print unless m/\A(?:^\|.\|..\|.\+$)\z/m"  infile > outfile

    Author Comment

    Thanks ps15.  It's removing the correct lines, but it's not then removing the blank lines.  Do you think we need to do a second run through the file to remove the blank lines?
    LVL 4

    Accepted Solution

    perl -ne "print unless (m/\A(?:^\|.\|..\|.\+$)\z/m or /^[\s\n\r]+$/)"  infile > outfile

    Author Comment

    Excellent!  Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now