Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Remove Single Blank Line Only - Keep Multiple Blank Lines

I have some data where I want to keep the multiple blank lines and remove the single blank lines only. My data look like this:

1

2

3

4


5


6

7

8



9

10

I would like the output to be:

1
2
3  
4


5


6
7
8



9
10

I have tried working with the following commands, but I can't seem to get it to work:

awk ' /^$/ { print; } /./ { printf("%s ", $0); }'

And

sed 'N;/^\n$/d;P;D'

Also, I tried using cat -s but that doesn't necessarily take out the blank lines. Furthermore, I have played around with sed '/^$/' but cant specify single lines only. Any help is greatly appreciated.

like image 530
DomainsFeatured Avatar asked Jun 21 '16 20:06

DomainsFeatured


People also ask

How do I get rid of unwanted blank lines in Word?

Click Edit Document > Edit in Word for the web. Empty paragraphs appear as blank lines in your document. To remove them, just select them and delete them.


2 Answers

Using gnu-awk it is pretty simple:

awk -v RS='\n+' '{printf "%s", $0 (length(RT) != 2 ? RT : "\n")}' file

1
2
3
4


5


6
7
8



9
10
  • Using -v RS='\n+' we constitute 1 or more line breaks as record separator
  • Using length(RT) we check how many line breaks are after each record
  • we print RT (original captured value) if length != 2

Alternative awk command:

awk -v RS='\n{3,}' '{gsub(/\n{2}/, "\n", $0); printf "%s", $0 RT}' file
like image 193
anubhava Avatar answered Oct 16 '22 14:10

anubhava


Using GNU sed (minor modifications needed for BSD/OSX):

$ sed -E ':a; N; $!ba; :b; s/([^\n])\n\n([^\n])/\1\n\2/g; tb' input
1
2
3
4


5


6
7
8



9
10

How it works

  • :a; N; $!ba

    Read the whole file in at once.

    Here, a is a label. N reads in the next line. $!ba branches back to label unless we are on the last line.

  • :b; s/([^\n])\n\n([^\n])/\1\n\2/g; tb

    Replace double-newlines with single newlines. Repeat as often as necessary.

    Here b is a label. The s command removes the double newlines. If any double-newlines were removed, then tb branches back to label b.

like image 37
John1024 Avatar answered Oct 16 '22 14:10

John1024