Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all Bash comments

Tags:

regex

bash

sed

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"
    
like image 701
Inventor Avatar asked Jan 24 '26 22:01

Inventor


1 Answers

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/'
like image 75
QWERTY21KG Avatar answered Jan 27 '26 18:01

QWERTY21KG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!