Went through a great many different utilities on Linux, in the end, it never came to anything. They all fall down on multi-line comments. Stopped on grep with the -E key. Is this way of getting all the C-style comments from the file correct?

Please advise a suitable utility and, if necessary, a regular program.

  • Maybe grep -Eoz "/\*+[^*]*\*+([^/*][^*]*\*+)*/" ? - Wiktor Stribiżew
  • First, what caused such a strange set of javascript , linux , grep tags? Secondly, your task is not very ordinary (pull out from the C code of the commenatry and it is in bash). I would like to know why bash and what are you going to do next with these comments? Will you continue to work with them in bash? - vihtor
  • @ WiktorStribiżew Yes, it works! You damn wizard! Please leave it as an answer so that I can accept it. - Kiryl Aleksandrovich
  • I don’t see the point of explaining the presence of linux / grep / regexp tags, and javascript is there, since 80% of the answers I’ve dug up before were thematically related to JS. To people working with JS, I suggested that this topic is closer. My task is, with the help of tags, to outline for myself the target audience. - Kiryl Aleksandrovich
  • In general, the JavaScript tag must be removed. Removed. - Wiktor Stribiżew

1 answer 1

Can use

 grep -Eoz "/\*+[^*]*\*+([^/*][^*]*\*+)*/" 

Options

  • E - sets the syntax of the regular POSIX ERE (uses the more regular and understandable most regular syntax, where + is a quantifier, not a + sign)
  • o - sets the mode of outputting found matches, but not whole lines
  • z - makes it possible to search for matches between several lines.

Regular expression

  • / - sign /
  • \*+ - 1 or more characters *
  • [^*]* - 0 or more characters other than *
  • \*+ - 1 or more characters *
  • ([^/*][^*]*\*+)* - 0 or more repeats
    • [^/*] is a character other than / and *
    • [^*]* - 0 or more characters other than *
    • \*+ - 1 or more characters *
  • / - sign /
  • Complicated. sed is able to nominally. - 0andriy
  • @ 0andriy sed can delete such comments well, sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' file sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' file sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' file . With the search for matches he is not very. sed -n '/\/\*/,/\*\//p' file does not give the correct result. - Wiktor Stribiżew
  • sed -n -e '/PATTERN_FROM/,/PATTERN_TO/p' , where PATTERN_FROM respectively the first line, and PATTERN_TO - the next in the block that you need to add somewhere. Learn to read the documentation, yes? - 0andriy
  • This is not working, I checked - Wiktor Stribiżew
  • It works, I also checked before writing here. - 0andriy