Reputation: 1506
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.
Upvotes: 6
Views: 1060
Reputation: 784918
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
Upvotes: 6
Reputation: 58361
This might work for you (GNU sed):
sed '/\S/!b;n;N;/^\s*\n\s*\S/D' file
If a line is empty print it. Otherwise, print the line and fetch the next two lines and if the first of these is empty but the second is not, delete the first and repeat.
Upvotes: 0
Reputation: 113814
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
.
Upvotes: 2