Shell – Grep for string but ignore commented out items with string

grepscriptingshelltext processing

I'm looking for the words gmail or outlook in a directory. Although there are some lines commented out with the word gmail and outlook in it. How can I grep for this?

I tried a bunch of things but I imagine it will look something like:

grep "^[^#;]" | egrep -i "gmail|outlook" *.ksh  > a.log

Best Answer

With grep:

grep -P '^(?=[\s]*+[^#])[^#]*(gmail|outlook)' file
  • -P activates Perl compatible regular expressions.
  • ^(?=...) defines a capture group which is not part of the pattern (lookahead; a Perl extenstion). The ^ means the start of the line.
    • Inside that group \s matches any whitespace character, *+ matches the whitespace 0 or more times and is greedy (a Perl extension).
    • [^#] matches any character which is not a #.
  • [^#]* outside the capture group we again match any character which is not a # 0 or more times
  • (gmail|outlook) finally match gmail or outlook

I made a test file with different examples:

$ cat file
# outlook
blah gmail # this should match
# gmail
  # gmail
        # foo
blah outlook # this should match
  outlook blah # gmail - this should match
foobar # gmail
        bar
        another gmail # this should match

The output is:

blah gmail # this should match
blah outlook # this should match
  outlook blah # gmail - this should match
        another gmail # this should match

Of course, you can run this on all your *.ksh files:

grep -P '^(?=[\s]*+[^#])[^#]*(gmail|outlook)' *.ksh  > a.log