In this post, I am going to present some simple tips when using grep.
Default Grep Command
I am using my custom grep command
grr by default whenever I would run
It is defined as follows:
The command is called
grr for “GRep Recursive”.
--recursive switch is self-explanatory.
--line-number option makes
grep prepend each match with the line where it was found.
--exclude switches can be changed depending on the actual needs.
There are tools dedicated specifically to search in source code folders.
However, the above alias can be used on any machine - not only within the personal development environment.
A word of caution:
Do not set an alias for
grep without changing the name, i.e. don’t do
alias grep="grep --recursive ...".
This may bite you in shell scripts where the author “blindly” uses
grep and thus your custom
grep is called.
Use Colors And Pipes
I have already written about
G as a better alias for grep when piping.
… allows to run:
An improved version of this alias keeps the color code from the input:
This is really handy when piping grep into grep like so:
color=alwyas set up
grep will keep the input color codes arriving at stdin.
I really like to pipe grep when it is easy to spell out the search pattern with multiple greps (“search for lines containing this but not that”).
Without that option, only the last grep would colorize - whatever the last pattern than was.
A Grep Wrapper
At work, there is a database dump containg lots of customer information. The file roughly looks like this:
I often find myself to search for data for a specific customer. For various reasons it is actually easier to grep for the customer in the dump file instead of querying the database. For this very end I have defined a dedicated shell function:
This function is obviously just a wrapper around a
grep with preset options.
I call it like so:
ls invocation is a hint to the age of the dump.
This could be achieved in other ways, too.
The point is that it is very handy to type and always accessible from the command line (where I spent my day).
This is not specific to
grep but rather a common pattern: A couple of commands belong together? A couple of command line arguments are always used in conjunction? Make a shell function or alias and automatize tedious work away.