Saturday, 10 May 2014

How to delete blank lines in a file ?

How to delete blank lines in a file ?

Ans- sample.txt | grep -v ‘^$’ > new_sample.txt

How it works ?

1. In grep ^ (caret) symbol matches the expression at the start of a line.
# ls | grep "^0"
01AF773_051_033.dd.xml
01AF773_051_033.html
01AF773_051_033.pd.sdd
01AF773_051_033.readme.txt
01AF773_051_033.rpm
01AF773_051_033.xml
01AL730_122_035.dd.xml
01AL730_122_035.html
01AL730_122_035.pd.sdd
01AL730_122_035.readme.txt
01AL730_122_035.rpm
01AL730_122_035.xml
01AM780_045_040.dd.xml
01AM780_045_040.rpm
01AM780_045_040.xml
01ZM780_040_040.dd.xml
01ZM780_040_040.rpm
01ZM780_040_040.xml
02ZB780_040_040.dd.xml
2. In grep $ symbol matches the expression end of the line..

#ls | grep "txt$"
01AF773_051_033.readme.txt
01AL730_122_035.readme.txt
aix_lpar.txt
ips.txt
lpar.txt
test.txt
testcheck.txt


So if we excute ... grep -v '^$' 
-v for non matching lines.
^$ will print the line which have started with EOL. (means the line where there is no contains..)



No comments:

Prasanta Kumar Behera. Mob- 8147470101