Coding, is? Fun!

Thursday, October 27, 2005

GREP for windows

I found the open source Grep for Windows application from sourceforge.net URL:
http://gnuwin32.sourceforge.net/packages/grep.htm
The install does not add grep.exe to the path in windows; I had to do it myself.
The documentation has examples and all the usage options.

C:>grep --help shows all the options and their uses.
I tried some different grep statements:
1. Find Grant statements in a .NET database project
grep -E GRANT -r -h *.*
-h is to eliminate preceding filename
-E is for the pattern (extended regular expression)
-r is for recursive
I used -E because that was the first one I found, but -G can be used too.

2.Find one word with another word in the same line, irrespective of order
For example, to find the words "Emacs" and "gnu" in the same line.
grep -r -i -E Emacs *.* | grep -E gnu

This actually uses the "pipe" or "|" character to feed the output of one statement to another.

3. To find lines with (Emacs AND gnu) OR (Emac AND org)
grep -r -i -E "Emacs" *.* | grep -r -i -E "gnu|org"

The pipe "|" in this case is part of the regular expression and can only be used within the double quotes. If it is used without the double quotes, the command shell would interpret "org" as a command and not part of the regular expression.
4. Exclude a file
grep -r -i --exclude-pattern.txt -E "Emacs" *.* | grep -r -i -E "gnu|org"

excludes pattern.txt from the list of files to search.

5.Get more lines AFTER the matching line
grep -r -i -A 5 --exclude-pattern.txt -E "Emacs" *.* | grep -r -i -E "gnu|org"

The -A 5 gets 4 more lines AFTER the matching line.
The -B 5 gets 4 more lines BEFORE the matching line.

2 Comments:

  • Hey, good looking blog here! I'm definitely going to bookmark you!

    I have a cheap traffic site/blog. It pretty much covers cheap traffic related stuff.

    Come and check it out if you get time :-)

    By Blogger Stacey, at 10:24 PM  

  • Hello, you have a great blog here! I'm definitely going to bookmark you.

    I have a currency trading recommendation site/blog.
    Full of currency trading recommendation information and help.

    Come and check it out, er, please :-)

    By Blogger kingpebble, at 10:25 PM  

Post a Comment

<< Home