Thursday, December 25, 2008

Remove duplicate blank lines - bash & awk


Input file:


$ cat file.txt


first line, two blank lines above
3 blank lines below


one more line,a blank line below

Two blank lines

a line
one more line
a blank line below

$


Let me put the line number at the beginning of each line of the above file to see the blank lines.

$ awk '{print NR,$0}' file.txt
1
2
3 first line, two blank lines above
4 3 blank lines below
5
6
7 one more line,a blank line below
8
9 Two blank lines
10
11 a line
12 one more line
13 a blank line below
14
$

Required: Remove duplicate blank lines from the above file i.e. make more than one consecutive blank lines to one single blank line.

Way1:

cat(1) gives an option named

-s, --squeeze-blank

which says : never more than one single blank line

$ cat -s file.txt

Way2:

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

Output:

first line, two blank lines above
3 blank lines below

one more line,a blank line below

Two blank lines

a line
one more line
a blank line below

$

No comments:

© Jadu Saikia www.UNIXCL.com