[How To] Remove Blank Lines From A File In Unix

[Unix] Remove Blank Lines From A File

Wish to remove blank spaces in text file ?? sed at your rescue again !!
Suppose you have a file newfile.txt with the below text.

$ more newfile.txt
MXGWGF678


 7689 MHGSTTW 78790


           JHKHYWT 90192 GZQPOI GTARQ

  HHYWTSH HBSHJSD JKDDD 777HGJKSDJ         HSGD886GJHT

And you wish to remove all the blank lines in the file and store the results in myresults.txt file.

$  sed '/^$/d' newfile.txt > myresults.txt

The result is as follows

$ more myresults.txt
MXGWGF678
 7689 MHGSTTW 78790
           JHKHYWT 90192 GZQPOI GTARQ
  HHYWTSH HBSHJSD JKDDD 777HGJKSDJ         HSGD886GJHT


To remove blank spaces from a text file, click on this link to read the tutorial.

Job accomplished !!