Fun with grep
September 25th, 2008
A few days ago, I was trying to get a list of all of the error codes we use in our XML-RPC exceptions. They all seemed to follow the pattern of calling into a superclass’ constructor, specifying the error code as the first parameter. For example:
1 | super(2754, "kickstartKeyAlreadyExists", ... |
I used my normal, admittedly a bit naive, approach:
1 | find -name '*Exception.java' -exec grep 'super(' {} \; |
In other words, find all classes that end in Exception.java and execute grep on them. The problem with this approach is the output:
1 2 | super(1025 , "notActiveSatellite" , ... super(2301 , "invalidPackageArch", ... |
It matched what I wanted it to, but it obscures the normal grep output of showing the file name. After some asking around in the #spacewalk Freenode chat room, we came up with two different ways to achieve what I wanted.
1 | find -name '*Exception.java' | xargs grep 'super(' |
I’ll leave it to the reader to check the man page for xargs for more details. As a quick summary, it’s a way of building up a command line based on an input. It’s syntax is also a ton nicer than using the -exec syntax of find.
A more to the point approach that doesn’t use find is:
1 | grep 'super(' -r ~/code/rhn/spacewalk/java *Exception.java |
One last approach that was suggested is similar:
1 | grep 'super(' **/*Exception.java |
The problem with the above is that it doesn’t work in bash; the original suggester likes to be weird and use zsh.
For the record, the output of the above looks as follows:
1 | ../xmlrpc/InvalidPackageException.java: super(2301, |
That’s just a snippet that would fit on the page correctly. The important aspect is that it shows both the file name and what was matched. In most cases, that’s significantly more helpful.

