You can use
GNU/BSD sed(1) and its neat commands ( since
-i is not defined by
POSIX )
Code:
sed -i '/^$/ {N; /^\n$/D;}' file
It processes each line as described below:
If we encounter a blank line ( «
/^$/ » ), then we load the next line from the file in the pattern space ( «
N » ), and we check whether the line corresponds to the pattern
/^\n$/ (only a newline) ; if the statement is true (in another words: if the pattern is matched, so if the line is strictly a newline), we remove the first line from the pattern space in case there are two blank lines ( «
D » ).
------
I also had another approach with
bash(1) for fun...
Code:
IFS=$'\n' read -rd '' -a lines < file
for ((i = 0; i < ${#lines[@]} - 1; i++)); do
printf '%s\n\n' "${lines[i]}"
done
printf '%s\n' "${lines[@]:(-1)}"