Remove blank lines in a text document

Posted on 2009-02-12
Last Modified: 2012-06-27
I have a txt document with many empty lines. How can I remove the empty lines in perl and Vi in linux.
In perl from command line I tried this: perl -ni -e "s/^n//" sample.txt  - this is not working.
Also can any one help me in getting solutoion f this in VI editor and perl from command line.,

2) in Linux in VI how can I get ^M charcter printed so that I can use s/^M// t replace empty liness
Question by:vkchaitu82
    LVL 84

    Accepted Solution

    perl -i -ne 'print if /\S/' sample.txt
    LVL 23

    Assisted Solution

    by:Maciej S
    To get ^M in vi press ctrl-v then ctrl-m (or just hit enter - it's ctrl-m :)).
    LVL 40

    Assisted Solution

    in vi editor and while in edit mode you can press cntrl-v followed by cntrl-m
    LVL 16

    Assisted Solution

    cat text.txt|grep -v "^$" >> newtext.txt

    this will remove all blank lines
    LVL 84

    Expert Comment

    is a line containing only whitespace considered a blank line?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now