As mentioned in comments above, what format 'comments' take in your use-case makes a difference. Still, for several cases, this may be enough, without having to create a script.
The solution:
Reading the question suggests you're using grep
already to search the files anyway, so pipe that through anothergrep
; like this:
grep your_pattern your_file | grep --perl-regexp --invert-match '(?:^;)|(?:^\s*/\*.*\*/)|(?:^\s*#|//|\*)'
What is not trapped:
This will still allow lines or that have a 'trigger' character elsewhere in the line, that have comments at the end, as in echo "Hello World" # another comment
, or that are part of a multi-line comment (except as noted in the explanation below.
If this is used as a post-filter to your grep these limitations should be negligible as most of the comments will still be filtered out and you won't worry "that your eyes glaze over" anymore.
The Explanation:
There are three patterns, which you can modify to suit your use-case if needed. The first (?:^;)
catches lines beginning with the ;
character. Must be first, without white space. The second catches lines that begin with the `/* ... */` comment style, with or without leading white space. The third
catches lines, with or without leading white space, that begin with #
, //
, or *
. The *
in the last pattern helps to catch the line inside a multi-line comment in the /* ... */
style where common style is to run a column of *
to connect the first and last line together. For example:
/************
*
* This is my
* multi-line
* comment.
*
************/
The (? ... )
notation around each pattern makes them 'non-capturing' patterns, hopefully to increase speed and reduce resource consumption. The -Pv
arguments to grep tell it to use Perl regular expression rules --perl-regexp
which allows the non-capturing grouping and allows the |
alternation operator to work, neither of which work in CLI grep. The grep man page does warn that the -P option is experimental, so do test before relying on it in your system. The --invert-match
tells grep
to reverse the match, returning lines that fail the pattern. These can be combined, and shortened to -vP
instead.
The reason to use this as a post-filter to your normal grep
is three-fold. First, you can do your normal grepping, and only add the extra work of using this when you run into your problem of too many comments in the output. (Less typing and fewer resources used.) Second, you have probably already developed the patterns you commonly use, and the habits that go with them, and adding more complexity to them could break them. Adding more work to debug patterns when you don't have to is wasted work. Third, It doesn't do well with multi-line comments at all, but if you've already grepped the file for what you want, then it'll remove most, if not all, comment from the results, and serve your purpose.
#
?/* ... */
?//
? – Jeff Schaller Jan 17 '17 at 01:41