I am grepping through some VB source files. I use the following query -

     grep -nr "GlobalCommArea\s*=\s*.*\.Transaction" . 

Some of the lines, however, are commented and begin with a single line comment. A single line comment in VB starts with a single quote - '

Unfortunately, I cannot simply remove all those lines that have a single quote in them, because some of the lines have a single quote comment after the line of code.

So I tried something like this to remove the single quote at the start of the line

     grep -nr "GlobalCommArea\s*=\s*.*\.Transaction" . | grep -v "^\s*'"

However, this did not work. How to do this?

1 Answer 1


You are using grep -n which prefixes matches with line numbers, so your second pattern "^\s*'" will never match. Instead, you can use one single regex:

grep -nr "^[^']*GlobalCommArea\s*=\s*.*\.Transaction" .

This searches for instances of your pattern not preceded by any single quotes.

  • Also, I'm pretty sure grep does not support \s to mean whitespace. Apr 16, 2012 at 21:17
  • @bonsaiviking The OP is probably using GNU grep, which does.
    – jw013
    Apr 16, 2012 at 21:18
  • 1
    @CodeBlue oh right I forgot the ^ anchor at the beginning. Fixed.
    – jw013
    Apr 16, 2012 at 21:37
  • 2
    @jw013 Source dive shows that feature was added in a later version than 2.5.4, which is what I have on Ubuntu 10.04. FYI Apr 16, 2012 at 21:39
  • 2
    @CodeBlue The first ^ is an anchor while the [^'] is a bracket expression.
    – jw013
    Apr 16, 2012 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .