четверг, 29 сентября 2011 г.

Useful bash macro: with

Here is a simple but quite nice bash macro that I use very often. Just added it into my ~/.bash_aliases:
function with () {
    # filter files list, leaving only files that has our key words
    xargs grep $@ | sed 's/:.*//' | uniq
}
Now my command line looks prettier:
$ cd project_dir
$ find . -name \*.java | wc -l # find all java files
over 9000
$ find . -name \*.java | with -i hitrin | wc -l # find all my java files
138
You can also use chaining:
find | with | with | ...
Exception: you can't use with with -v option of grep, because it displays rows in another format. Sad but true.