Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the regex '/^$/d' mean?

Tags:

regex

unix

I was trying to remove blank lines in a file using bash script. Now when i was searching in the INTERNET, i came across two variations of it. In one, we can directly modify the source file and in the other we can store the out put in another file . Here are the code snippets :

sed -i '/^$/d' fileName.txt

sed '/^$/d' fileName.txt > newFileName.txt

What i could not understand is how the regex '/^$/d' can be interpreted as blank lines. I am afraid i am not good in regex . Can some one explain me this one ?

Also is there some other way to do it ?

like image 564
The Dark Knight Avatar asked Apr 03 '13 19:04

The Dark Knight


1 Answers

/^$/d


/ - start of regex
^ - start of line
$ - end of line
/ - end of regex
d - delete lines which match

So basically find any line which is empty (start and ending points are the same, e.g. no chars), and delete them.

like image 194
Marc B Avatar answered Oct 29 '22 11:10

Marc B