Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»How to Remove Empty Lines from File

    How to Remove Empty Lines from File

    RahulBy RahulJuly 8, 20132 Mins Read

    Some time we need to remove empty lines from a file. Its can be done manually if file have few lines but if file have thousands of line this is hard to be done manually. Use one of following method to remove empty lines from a file.

    Method 1 – Using sed

    Sed is an stream editor. We can easily remove all blank lines using sed command. Use one of following sed command to remove blank lines from file. For example main.txt is your original file from which you need to remove blank lines.

    Below command will remove all blank line and save content in seconf file out.txt. It will not affect the original file.

    # sed '/^$/d' main.txt > out.txt
    

    Now if you want to make changes in original file using -i switch sed command.

    # sed -i '/^$/d' main.txt
    
      -i ( edit files in place ) Used for make changes in same file.

    Method 2 – Using perl

    Instead of sed, you can also use perl (a programming languege) to remove blank lines. Use the below example command to remove blank lines from main.txt file.

    # perl -i -n -e "print if /S/" main.txt
    

    Method 3 – Using awk

    Also you can use AWK command line tool to remove blank lines from a file. For example use below command.

    # awk 'NF > 0' main.txt > out.txt
    
    files
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Debug a Shell Script?
    Next Article How to Set Up MySQL Master-Slave Replication

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    1 Comment

    1. frank on March 21, 2019 5:40 pm

      # perl -i -n -e “print if /S/” main.txt
      not working for me

      # perl -i -n -e “print unless /^$/” main.txt

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.