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
grep
process, assuming your implementation of grep can use Perl regexps. (Gnugrep
can, but for example the defaultgrep
on an OSX machine can't.) To do it in the way you were trying, you'd dogrep "^[^#;]" *.ksh | egrep -i "gmail|outlook" > a.log
, orgrep -v "^[#;]" *.ksh | egrep -i "gmail|outlook" > a.log
.