CyberKeeda In Social Media

BASH : How to delete blank lines from a file using SED.






A quick one liner to delete blank lines from a file

cybeerkeeda@Linux-Maniac:~ cat /tmp/hosts.txt

myhost0058x

myhost0070x

myhost0077x

myhost0078x

myhost0079x


I will be using this file later on within LOOPS and intended blank spaces will be avoided.
Below mentioned SED one liner will sort out the requirement
cybeerkeeda@Linux-Maniac:~ sed -i '/^$/d' /tmp/hosts.txt

myhost0058x
myhost0070x
myhost0077x
myhost0078x
myhost0079x


Now let's take another example of a file, that contains TAB or Space at the starting of line.
In case if you need to remove the lines that contains extra spaces/Tabs from the file's content you can again use SED for it.

myhost0058x
        192.168.0.101
myhost0070x
        192.168.0.101
myhost0077x
        192.168.0.101
myhost0078x
        192.168.0.101
myhost0079x



cybeerkeeda@Linux-Maniac:~ sed  '/^[[:space:]]/d' /tmp/hosts.txt
And it will remove all the lines starting with extra Space at the beginning.

myhost0058x
myhost0070x
myhost0077x
myhost0078x
myhost0079x

No comments:

Post a Comment

Designed By Jackuna