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.
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.
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
-v RS='\n+'
we constitute 1 or more line breaks as record separatorlength(RT)
we check how many line breaks are after each recordRT
(original captured value) if length != 2
Alternative awk
command:
awk -v RS='\n{3,}' '{gsub(/\n{2}/, "\n", $0); printf "%s", $0 RT}' file
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
: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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With