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.
Just pipe your results into grep:
$ some pipeline | of commands | grep
When no filename is supplied to grep, it reads from standard input.
Most welldesigned 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 <email@example.com>
You could have re-typed the first grep, but this example also shows the power of the !! history operator.
The !! let’s you repeat the previous command without retyping it.
You can then continue adding to the command line after the !! as we show here.
The shell will display the command that it runs, so that you can see what you got as a result of the !! substitution (see Recipe 18.2, “Repeating the Last Command”).
You can build up a long grep pipeline very quickly and simply this way, seeing the results of the intermediate steps as you go, and deciding how to refine your search with additional grep expressions.
You could also accomplish the same task with a single grep and a clever regular expression, but we find that building up a pipeline incrementally is easier.