0

How to write linux's grep regex to match multi line comment like this

/*
comment 
body
*/

so grep matches 4 lines in the case as above. Note any of the single lines of the comment can start with white chars (for example tab or spaces)

I do not mind if the regex pattern also matches single line comment

/* comment body */

but this is not required since I already have a pattern addressing that need.

If matching multi line commend by using grep is not doable then I can use sed, awk, etc. But grep with its regex is preferred.

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • You can't. `grep` only operates on one line at a time. – Barmar May 16 '19 at 19:39
  • Possible duplicate of [Regex to match a C-style multiline comment](https://stackoverflow.com/q/13014947/608639), [Regular expression to find C style block comments](https://stackoverflow.com/q/16160190/608639), etc. – jww May 16 '19 at 19:50
  • @Barmar how about sed/awk? do they also work one line at the time? Using multi line switch in regex with grep will not make any difference? – Jimmix May 16 '19 at 19:56
  • `awk` allows you to set variables and refer back to them. So when you match the start of a comment you can set a variable, and then other statements can check that variable and behave accordingly for the lines after it. When you match the end, you unset the variable. – Barmar May 16 '19 at 19:58

0 Answers0