grep: The time has come

I saved grep for last because grep deserves the attention. grep is one of those tools that probably deserves its own spot in history, like sed or rsync. It’s just that powerful.

You probably know what grep does — screens through streams of text and spits out matching results. So at its most basic,

grep Text file.txt

should highlight lines in file.txt that match “Text” … simple, isn’t it?

grep makes it simple, that’s for sure. Have three files and you’re not sure where something is?

grep Text file-01.txt file-02.txt file-03.txt

grep finds it, adding the name of the file on the front, whenever “Text” appeared.

So what else can grep handle? How about numbering results, so I don’t have to pick through lines by hand?

grep -n Text file.txt

Yes, numbers! But wait, the numbers are the lines “Text” appears in, not the number in the list. Keep that in mind.

There’s another option that might not seem valuable. Here’s the -o flag, which plays havoc on my test file.

grep -o ie test.txt

Output looks like this:

ie
ie
ie
ie

Well that’s dumb. Or not. Here’s the same thing with the line numbering option:

grep -on ie test.txt

And you get:

36:ie
54:ie
97:ie
100:ie

So sometimes, for example on long lines of text, maybe I don’t care about the actual output, only the line it appears on. Easier to find typos, I should think.

What’s next … how about -A, -B and -C. A will show lines after the matching string, B shows before the string, and C is sort of a combination of A and B. Ergo,

grep -A2 ie test.txt

yields:

coefficients
guildhall
customs
--
energies
slurring
benight
--
ladies
infringements
meets
metier

Note that the second and third lines in each set there don’t necessarily contain the “ie” pair, but I asked that they be included. grep adds the double dashes.

And that last line, “metier”? Where’s its following couple? Nowhere. That’s the end of the file, and it’s only three lines after the word “ladies,” so it appears to be a string of four.

Probably the most important flag is -v for the simple reason that it reverses the results — instead of finding matches, grep reports nonmatching lines. To wit:

grep -vn e test.txt

And the results include …

78:bursty
82:lobby
83:harrows
85:skulk
91:propyl
93:cowling
94:tracy
95:unflagging

And so forth, with no letter “e” in a line.

grep comes into play a lot more than I would think. The Index page for this site is built off a massive XML export, and I need grep to skim through it all and find the links.

Or, here’s a list of movies, skimmed to find anything from the 1940s.

grep 194 list-01.txt

Yielding …

Fantasia (1940)
It's a Wonderful Life (1946)
Notorious (1946)
The First of the Few (1942)
The Great Dictator (1940)

Try piping directory lists through grep; it might be easier to handle than ls at times. I’ve seen other sites that use grep in conjunction with find as a kind of screen that displays directory paths. Clever.

I should mention that grep comes with egrep and fgrep, the former handling regular expressions more deftly than the -E flag for regular grep. The latter is a “faster” grep, and might be more useful for those really, really big searches.

One more flag, and I’m done. You guessed it: --color !

2013-11-17-lv-r1fz6-grep

Nothing is complete without color. 😉

22 thoughts on “grep: The time has come

  1. Sam Rowe

    Your brace expansion is technically being handled by the shell, not grep. I mention it because it might be confusing to new users.

    1. K.Mandla Post author

      Yes, that’s a bad example, actually. I should edit that to a list of files, instead of the range. Thanks for the catch.

  2. Pingback: less: Do more with less | Inconsolation

  3. Pingback: ngrep: Does for the ‘net what grep does for … | Inconsolation

  4. Pingback: Bonus: P is for pushing up daisies | Inconsolation

  5. Pingback: sort: Deserves better attention | Inconsolation

  6. Pingback: cheat, cheat, cheats and cheat: So many cheaters | Inconsolation

  7. Pingback: clog: Custom color for logs | Inconsolation

  8. Pingback: acoc: More filter-based colorizing for the terminal | Inconsolation

  9. Pingback: ps: That other process tool | Inconsolation

  10. Pingback: mediainfo: There is none higher | Inconsolation

  11. Pingback: ack: A grep for programmers | Inconsolation

  12. Pingback: ngp: Find it and edit it, in one deft motion | Inconsolation

  13. Pingback: ngp: Find it and edit it, in one deft motion | Linux Admins

  14. Pingback: the_silver_searcher: Intergalactic searcher smackdown | Inconsolation

  15. Pingback: the_silver_searcher: Intergalactic searcher smackdown | Linux Admins

  16. Pingback: wikipedia2text: Looking well-preserved, thanks to Debian | Inconsolation

  17. Pingback: Tricks of the trade | Motho ke motho ka botho

  18. Pingback: pup: Playing fetch with HTML | Inconsolation

  19. Pingback: pup: Playing fetch with HTML | Linux Admins

  20. Pingback: note: A noteworthy application | Inconsolation

  21. Pingback: note: A noteworthy application | Linux Admins

Comments are closed.