• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Did you find what you needed?
    Show your support and gratitude by making a contribution!!
    Life always offers you a second chance ... Its called tomorrow !!!

    Monday, September 11, 2017

    sed: delete all blank lines from a text file

    Here our requirement is to remove all the blank lines from a file.





    Our sample file
    [root@golinuxhub ~]# cat /tmp/file
    This is line one

    This is line two
    This is line three

    This is line four

    [root@golinuxhub ~]#

    Use the below command to remove all the blank lines from a text file
    [root@golinuxhub ~]# sed -e '/^$/d' /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four
    [root@golinuxhub ~]#

    Here the regular expressions used are
    ^ - means starting with
    $ - means ending with

    Since we did not provided any other other character between our regular expressions, this is a notation to consider it as blank line.

    To perform the action in the same file use "-i"
    [root@golinuxhub ~]# sed -i '/^$/d' /tmp/file

    [root@golinuxhub ~]# cat /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four


    I hope the article was useful.















    0 comments:

    Post a Comment