Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate blank lines with awk

Tags:

awk

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!

like image 815
curt924 Avatar asked Jan 25 '23 04:01

curt924


1 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
like image 56
Ed Morton Avatar answered Feb 05 '23 16:02

Ed Morton