Doing a Search in a Pipeline

Problem

You need to search for some text, but the text you’re searching for isn’t in a file; instead, it’s in the output of a command or perhaps even the output of a pipeline of commands.

Solution

Just pipe your results into grep:

$some pipeline | of commands | grep

Discussion

When no filename is supplied to grep, it reads from standard input. Most well-designed utilities meant for shell scripting will do this. It is one of the things that makes them so useful as building blocks for shell scripts.

If you also want to have grep search error messages that come from the previous command, be sure to redirect its error output into standard output before the pipe:

$ gcc bigbadcode.c 2>&1 | grep -i error

This command attempts to compile some hypothetical, hairy piece of code. We redirect standard error into standard output ( 2>&1) before we proceed to pipe (|) the output into grep, where it will search case-insensitively (-i) looking for the string error.

Don’t overlook the possibility of grepping the output of grep. Why would you want to do that? To further narrow down the results of a search. Let’s say you wanted to find out Bob Johnson’s email address:

$ grep -i johnson mail/* ... too much output to think about; there are lots of Johnsons in the world ... $ !! | grep -i robert grep -i johnson mail/* | grep -i robert ... more manageable output ... $ !! | grep -i "the bluesman" grep -i johnson mail/* | grep -i robert | grep -i "the bluesman" Robert M. Johnson, The Bluesman ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.