How can I expand arguments to a bash function into a chain of piped commands?

I often find myself doing something like this a lot:

something | grep cat | grep bat | grep rat

when all I recall is that those three words must have occurred somewhere, in some order, in the output of something...Now, i could do something like this:

something | grep '.*cat.*bat.*rat.*'

but that implies ordering (bat appears after cat). As such, I was thinking of adding a bash function to my environment called mgrep which would turn:

mgrep cat bat rat

into

grep cat | grep bat | grep rat

but I'm not quite sure how to do it (or whether there is an alternative?). One idea would be to for loop over the parameters like so:

while (($#)); do
    grep $1 some_thing > some_thing
    shift
done
cat some_thing

where some_thing is possibly some fifo like when one does >(cmd) in bash but I'm not sure. How would one proceed?

Answers


I believe you could generate a pipeline one command at a time, by redirecting stdin at each step. But it's much simpler and cleaner to generate your pipeline as a string and execute it with eval, like this:

CMD="grep '$1' "  # consume the first argument
shift

for arg in "$@"   # Add the rest in a pipeline
do
  CMD="$CMD | grep '$arg'"
done
eval $CMD

This will generate a pipeline of greps that always reads from standard input, as in your model. Note that it protects spaces in quoted arguments, so that it works correctly if you write:

mgrep 'the cat' 'the bat' 'the rat'

Thanks to Alexis, this is what I did:

function mgrep() #grep multiple keywords
{
    CMD=''
    while (($#)); do
        CMD="$CMD grep \"$1\" | "
        shift
    done
    eval ${CMD%| }
}

Need Your Help

Facebook like button skewing Google Analytics page time load

google-analytics facebook-like

I have a pretty well performing site with an average load time of 0.97 thorough all the pages. However, we've recently added a Facebook like button to our homepage and it resulted in load time of 2...

What are all the Tritium environment variables?

moovweb tritium

Where can I find a full list of the Tritium variables (such as $host, $path, $content_type, etc.)?