I met some old Makefile script which was designed to delete blank lines using the sed command. The code itself is pretty easy and straightforward.
sed -i -e '/^[ \t]*$$/ d'
I know the ^[ \t]*$ part just indicates that a line starts with space or tab then repeated zero or more times until the end of the line. I didn't quite understand why there is an additional "$" sign at the end of the regular expression.
I also tried using only single $ sign. It seems that the same effects can be achieved.
sed -i -e '/^[ \t]*$/ d'
Then what is the purpose of using the double-dollar sign in this case?
-------------------------Additional comments--------------------
It's my fault that I didn't mention that it comes from a Makefile. I naively thought it would be the same thing no matter it is inside or outside a Makefile. The command is like this:
RM_BLANK: org_file
@cpp org_file | sed -e 's/ */ /g' > file
@sed -i -e '/^[ \t]*$$/ d' file
org_file is the file that contains a lot of blanks lines.
It (I mean with $$) behaves exactly as hek2mgl's answer below has predicted when used outside a Makefile if the sed command is performed directly on org_file. It only deletes lines that end with a $ and leaves the empty lines without $ intact. But when used in a Makefile environment, it simply deletes blank lines that don't have a $ at the end of line. I think it might have to do with the Makefile convention. Would someone help with this puzzle?
It's not a safety mechanism, it's just part of the regular expression pattern. In basic posix regular expressions, which sed is using, the $ has no special meaning, except of when being used at the end of the pattern.
This means that the expression matches such lines that contain only a literal dollar sign $, but this can be optionally prefixed by whitespace or tabs.
If you remove the second $, the sed command would remove empty lines, which optionally contain whitespace or tabs but don't end with a $.
http://man7.org/linux/man-pages/man7/regex.7.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With