removing duplicate blank lines

Hi,

how to remove the blank lines from the file only If we have more than one blank line.

thanks
rameez

awk '/^$/ { if (blank++) next; print }1' file

It woks fine.

Can you please explain the following part in the command
'{ if (blank++) next; print }1'

The variable blank will be zero initially, but non-zero (i.e. true) on subsequent runs.

The 1 is an awk idiom which matches and prints lines not handled by previous conditions.

Actually you should change it to '{ blank=0; print }' in order for the script to work correctly on files where there are multiple sequences of adjacent empty lines. Sorry for missing that.

Thanks...thats great!

Another way with awk yet :

awk 'NR==1 || NF || prvNF {print} {prvNF=NF}'

Jean-Pierre.

Sorry, it's still not right. Should test before I post. Apologies.

awk '/^$/{ if (! blank++) print; next } { blank=0; print }'file 

(What a grand way to increase my post count ...)

From the cat(1) manpage

cat -s file
1 Like

:b:
On my AIX box, I must use -r or -S

Jean-Pierre.