Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To remove blank lines in data set

Tags:

shell

sed

awk

perl

I need a one liner using sed, awk or perl to remove blank lines from my data file. The data in my file looks like this -

Aamir
Ravi 

Arun


Rampaul
Pankaj

Amit

Bianca

These blanks are at random and appear anywhere in my data file. Can someone suggest a one-liner to remove these blank lines from my dataset.

like image 514
Angelo Avatar asked Dec 04 '22 06:12

Angelo


2 Answers

it can be done in many ways.

e.g with awk:

awk '$0' yourFile

or sed:

sed '/^$/d' yourFile

or grep:

grep -v '^$' yourFile
like image 188
Kent Avatar answered Dec 20 '22 13:12

Kent


A Perl solution. From the command line.

$ perl -i.bak -n -e'print if /\S/' INPUT_FILE

Edits the file in-place and creates a backup of the original file.

like image 40
Dave Cross Avatar answered Dec 20 '22 14:12

Dave Cross