Remove duplicate empty lines using sed

Use sed to remove duplicate empty lines from a file and replace them with a single empty line.

remove_duplicate_empty_lines() {
    sed '/^$/N;/^\n$/D'
}
sed -i "" '/^$/N;/^\n$/D' input.txt

Example:

$ cat --number file.txt
     1    1
     2
     3
     4
     5    5
     6    6
     7
     8
     9    9
 
$ cat file.txt | sed '/^$/N;/^\n$/D' > modified.txt
 
$ cat --number modified.txt
     1    1
     2
     3    5
     4    6
     5
     6    9
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .

Comments

Leave a Reply