How can I match and delete all comments from the line? I can delete comments starting from new line, or the ones not in quotes using sed. But my script fails in the following examples
This one "# this is not a comment" # but this "is a comment"
Can sed handle this case? if yes what is the regex?
Example:
Input:
This one "# this is not a comment" # but this "is a comment"
Output:
This one "# this is not a comment"
If we assume that # is not a comment when it is in quotes or escaped with backslash, then we can define the following regex:
(ES|RT|QT)*C?
where
ES - escape sequence: \ followed by 1 char
\\.
RT - non-special regular text
[^"\\#]*
QT - text in quotes
"[^"]*"
C - comment starting with unescaped, unquoted hash sign # and ending with the end of line
#.*
The possible solution using sed:
sed 's/^\(\(\\.\|[^"\\#]*\|"[^"]*"\)*\)#.*$/\1/'
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