tail -f findings.out

« Watching a fork bomb explode

A better Ruby prompt »

Useful grep incantations

grep is one of the core utilities on nearly every *nix-based system. If you’ve been using Linux for more than the most casual activities, you’ve probably used it. While its basic use is quite simple, there are a few additional options and related utilities that can come in handy.

Don’t pipe cat’ed files to grep

Before covering the options, I’d like to note that it’s not necessary to cat a file and pipe its contents to grep in order to use it. For some reason this tends to be the early way people, including me, use grep:

1
cat file.txt | grep "string"

Instead, skip the middleman and send the file right to grep:

1
grep "string" file.txt

This has the same result, and saves you some characters.

Basic options

These are the more common, but still quite useful, options:

  • -i: Perform case-insensitive matching
  • -r: Look in subdirectories in the specified directory recursively
  • -v: Invert match set; useful for filtering out matches
  • -e PATTERN: Search for the specified regex pattern (The regex set in grep is somewhat limited. For a more full-featured implementation, check out egrep.)
  • -A NUM / -B NUM: Include NUM lines of context before (-B) and/or after (-A) the matching lines. -C NUM allows you to specify the number of lines to be included before and after matches.

Additional options

Here are some less common options and groups that I’ve found quite useful:

  • -l (lower case L): This only returns the filenames of files containing matches. To add filenames to the results and also show the matching lines, use -H.
  • -o: Returns only the matched part of matching lines. Useful with -i to see all case variations of a term. It’s also useful to include -n, to show the line numbers of the matching lines.
  • Putting these together, this recursively finds instances of FOO in current directory and children:
    1
    grep -Hnr FOO *

    This returns: FILENAME:LINENUM:LINE. And since I don’t like remembering groups of options, I created an alias:

    1
    alias grepword="grep -Hnr"

    grepword-example

  • -w: Only show lines matching with whole words, not parts of words.
  • -c: Instead of piping results to wc -l to find the number of matches, use -c to display this.

Coloration

I love using color on the command prompt to make the information presented clearer. grep facilitates this with two options. These can be set in ~/.bashrc:

1
2
3
4
# Turn on grep coloration:
export GREP_OPTIONS='--color=auto'
# Specify a light green color for matching words:
export GREP_COLOR='1;32'

Then run “. ~/.bashrc” to source the file, and try out your new grep:
grep coloring
These same values may be specified through command line options as well. See this page for details. For selection the right color, check out this reference script.

While grep is quite wonderful, there is actually something better for certain uses: ack-grep. Read more about it in this post. For everyday searching through files, I’ve found ack-grep to be much easier to use and more informative. It’s still essential to understand the original grep as well, in order to construct useful and interesting scripts.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Useful ways to list directory contents
  2. More efficient HTML editing in vim
  3. Get useful image information on the command line
  4. Easily sum matching file line counts
  5. Converting multiple images to one PDF on Linux

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: ,
June 11, 2009 - 1:03 AM
2 comments »
Leave a reply

Subscribe without commenting

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.