Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

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
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
show 7 more comments

Your Answer

 
discard

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.