Removing duplicate blank lines with awk

Viewed 224

For one of my problems for class, I have a file where I am to delete duplicate blank lines in a file. so for example, I have an input file that looks like this:

Sample Line 1



Sample line 2

Sample line 3

and the output would then turn all multiple blank lines into a singular one, so the output file would look like this:

Sample Line 1

Sample line 2

Sample line 3

I've been able to complete this with a sed command, but the problem insists that I use awk in order to obtain this output. The closest I've gotten has been with awk '!x[$0]++', but that simply deletes pretty much every blank line. I feel like I'm missing something basic.

Thanks for any help!

3 Answers
$ awk 'NF{c=1} (c++)<3' file
Sample Line 1

Sample line 2

Sample line 3

or if you don't mind an extra blank line at the end:

$ awk -v RS= -v ORS='\n\n' '1' file
Sample Line 1

Sample line 2

Sample line 3

Could you please try following.

awk '!NF{found++} found>1 && !NF{next} NF{found=""} 1'  Input_file

Output will be as follows.

Sample Line 1

Sample line 2

Sample line 3

This also works if the file has duplicate lines at beginning or end.

awk '
NF==0{
    if (! blank) {print;blank=1}
    next
}
{blank=0;print}
' file

The base for its operation is that NF is zero for every blank/empty line with the default awk separator.


For example, if file is:



Sample Line 1



Sample line 2

Sample line 3
Sample line 4


it becomes


Sample Line 1

Sample line 2

Sample line 3
Sample line 4

Related