1

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?

4

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. – bonsaiviking Apr 16 '12 at 21:17
  • @bonsaiviking The OP is probably using GNU grep, which does. – jw013 Apr 16 '12 at 21:18
  • 1
    @CodeBlue oh right I forgot the ^ anchor at the beginning. Fixed. – jw013 Apr 16 '12 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 – bonsaiviking Apr 16 '12 at 21:39
  • 2
    @CodeBlue The first ^ is an anchor while the [^'] is a bracket expression. – jw013 Apr 16 '12 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.