Delete blank rows in a dataset

I need one liner using sed, awk or perl to remove blank lines from my data file. The data in my file is as follows:

Aamir Ravi Arun Rampaul Pankaj Amit Bianca 

These spaces are in random and appear somewhere in my data file. Can anyone suggest one liner to remove these blank lines from my dataset.

+4
source share
6 answers

This can be done in many ways.

e.g. with awk:

 awk '$0' yourFile 

or sed:

 sed '/^$/d' yourFile 

or grep:

 grep -v '^$' yourFile 
+6
source

Perl solution. From the command line.

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

Edits the file in place and backs up the source file.

+4
source

AWK Solution:

Here we scroll through the input file to check if they have any field. NF is an AWK built-in variable that is set for the number of fields. If the line is empty, then NF is not specified. In this one liner we check if NF is true, i.e. The value is set. If so, we print a line that is implicit in AWK when the template is true.

 awk 'NF' INPUT_FILE 

SED Solution:

This solution is similar to those mentioned as an answer. As a syntax, we do not print any lines that are empty.

 sed -n '/^$/!p' INPUT_FILE 
+2
source

for perl it is easier than sed, awk or grep.

$ cat tmp / tmpfile Aamir Ravi

Arun

Rampaul Pankai

Amit

Bianca

$ perl -i -pe ' s {^ \ s * \ n $} {} ' tmp / tmpfile

$ cat tmp / tmpfile Aamir Ravi Arun Rampaul Pankai Amit Bianca

+2
source

You can do:

  sed -i.bak '/^$/d' file 
+1
source

Perl Solution:

 perl -ni.old -e 'print unless /^\s*$/' file 

... which are created as a backup of the source file, labeled '.old'

+1
source

Source: https://habr.com/ru/post/1383040/


All Articles