Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
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
@CodeBlue oh right I forgot the ^ anchor at the beginning. Fixed. – jw013 Apr 16 '12 at 21:37
@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
@CodeBlue The first ^ is an anchor while the [^'] is a bracket expression. – jw013 Apr 16 '12 at 21:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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