Suppose we have a text file contain a lot of blank lines like the example below :
tedy@tedy-laptop:~$ cat test asia amerika afrika australia jakarta Indonesia
Sometime we need to remove all blank lines and grab only the text content. To do that we can use the awk utility :
tedy@tedy-laptop:~$ awk 'NF > 0' test asia amerika afrika australia jakarta Indonesia tedy@tedy-laptop:~$
Another example, we have a text file like this :
tedy@tedy-laptop:~/Desktop$ cat test.txt
eth0 Link encap:Ethernet HWaddr 00:17:42:bd:fc:f8
inet addr:192.168.131.158 Bcast:192.168.131.255 Mask:255.255.255.0
inet6 addr: fe80::217:42ff:febd:fcf8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:554121 errors:0 dropped:2 overruns:0 frame:0
TX packets:460920 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:440871435 (440.8 MB) TX bytes:384609487 (384.6 MB)
Interrupt:16
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:81280 errors:0 dropped:0 overruns:0 frame:0
TX packets:81280 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:77083351 (77.0 MB) TX bytes:77083351 (77.0 MB)
wlan0 Link encap:Ethernet HWaddr 00:1f:3b:7e:a1:ef
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wmaster0 Link encap:UNSPEC HWaddr 00-1F-3B-7E-A1-EF-00-00-00-00-00-00-00-00-00-00
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
We can remove all blank lines using grep utility :
tedy@tedy-laptop:~/Desktop$ cat test.txt | grep -v '^$'
eth0 Link encap:Ethernet HWaddr 00:17:42:bd:fc:f8
inet addr:192.168.131.158 Bcast:192.168.131.255 Mask:255.255.255.0
inet6 addr: fe80::217:42ff:febd:fcf8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:554121 errors:0 dropped:2 overruns:0 frame:0
TX packets:460920 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:440871435 (440.8 MB) TX bytes:384609487 (384.6 MB)
Interrupt:16
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:81280 errors:0 dropped:0 overruns:0 frame:0
TX packets:81280 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:77083351 (77.0 MB) TX bytes:77083351 (77.0 MB)
wlan0 Link encap:Ethernet HWaddr 00:1f:3b:7e:a1:ef
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wmaster0 Link encap:UNSPEC HWaddr 00-1F-3B-7E-A1-EF-00-00-00-00-00-00-00-00-00-00
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
tedy@tedy-laptop:~/Desktop$