Friday, August 10, 2007

Removing blank lines using grep

When you have something like this:
[root abcux]/tmp # cat lines.txt
line 1

line 3

line 4




line 9


line 10

#line 12


#line 15
line 16

[root abcux]/tmp #


but you want to exclude the blank lines and line starting with "#", do this...

[root abcux]/tmp # cat lines.txt | egrep -v "^#" | egrep -v "^$"
line 1
line 3
line 4
line 9
line 10
line 16
[root abcux]/tmp #


--- Update at 15:19
Another set of trick is to issue this command (thanks yoe!)
[root abcux]/tmp # cat lines.txt | egrep -v "^#" | egrep "."
line 1
line 3
line 4
line 9
line 10
line 16
[root abcux]/tmp #


Tadaa.

1 comment:

yoe said...

another way of getting rid of blank lines

#cat line.txt | egrep -v "^#" |egrep '.'

bilo nak mai minum2 sinih? pagi2 aku tadak geng la heheheh