I'm writing a bash script where I need to strip comments from Lua files, which are formatted:

--like this

foo="bar" --or like this

--[[ or like this ]]


I know I can use sed 's/--.*$//' ${my_file} to remove the single-line comments, but how can I address the multi-line one?


2 Answers 2


Nope, not really the job for a regular expression engine. For example, you can have -- or ]] perfectly legally in strings. And a string ends with ", but not with \".

The idea of programming language doesn't map easily (in some cases, not at all, but this becomes an exercise in grammar theory) to regular expressions.

I'm not a language theorist myself, but I think lua might not define a regular grammar and hence might not be regex-parseable.

The way you'd implement this is by building a lexer, which reads the file, character for character, and divides them into tokens (like, this is a string token, which you start when you see a " and ends when you see an unescaped "). You emit all tokens that are not comments.

  • That makes sense, although I was hoping that wasn't the answer since parsing (obviously) isn't one of my strong skills. String literals containing -- may be an edge case for me, so I think the other answer might be good enough. Thank you for your advice!
    – ridgek
    Commented Nov 25, 2021 at 22:50

Assuming all multi line matches start with --[[ and end with ]], you can use a range match to address the multi-line as well as single line matches.

$ sed '/^--\[\[/,/\]\]/d' input_file
--like this

foo=bar --or like this

Implementing your original code can now clear the remaining comments that do not match the previous command.

$ sed '/^--\[\[/,/\]\]/d;s/--.*$//' input_file


To clean up, the empty lines can also be removed.

$ sed '/^--\[\[/,/\]\]/d;s/--.*//;/^$/d' input_file
  • yes, that's fine until the first person uses -- in a string. Commented Nov 25, 2021 at 10:43
  • Thank you both! I was able to come up with this, which seems to work: lua --like this foo="bar" --or like this bar="--not like this" baz="--[[ not like this ]]" --[[ or like this ]] --[[ or like this ]] shell $ sed -e '/^--\[\[/,/\]\]/d' -e 's/^--.*//' -e 's/[^\"]--.*//' -e '/^$/d' test.lua foo="bar" bar="--not like this" baz="--[[ not like this ]]"
    – ridgek
    Commented Nov 25, 2021 at 23:07
  • Oops, I didn't realize you can't format a code block in a comment. Anyway, the one liner is: sed -e '/^--\[\[/,/\]\]/d' -e 's/^--.*//' -e 's/[^\"]--.*//' -e '/^$/d' test.lua
    – ridgek
    Commented Nov 25, 2021 at 23:12
  • I forgot about single-quotes. sed -e '/^--\[\[/,/\]\]/d' -e 's/^--.*//' -e 's/[^\"'\'']--.*//' -e '/^$/d' test.lua
    – ridgek
    Commented Nov 26, 2021 at 3:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .