views:

101

answers:

3

Some commands like svn log, for example will only take one input from the command line, so I can't say grep 'pattern' | svn log. It will only return the information for the first file, so I need to execute svn log against each one independently.

I can do this with find using it's exec option: find -name '*.jsp' -exec svn log {} \;. However, grep and find provide differently functionality, and the -exec option isn't available for grep or a lot of other tools.

So is there a generalized way to take output from a unix command line tool and have it execute an arbitrary command against each individual output independent of each other like find does?

A: 

try xargs:

grep 'pattern' | xargs svn log
Tristan Su
I voted this back up. Why the downvote? svn log does take more than one argument, and while this answer does not exactly answer the question, it may in fact be a better way to use xargs.
Zan Lynx
+8  A: 

The answer is xargs -n 1.

echo moo cow boo | xargs -n 1 echo

outputs

moo
cow
boo
Kinopiko
thanks! exactly what i needed.
Keith Bentrup
A: 

A little one off shell script (using xargs is much better for a one off, that's why it exists)

#!/bin/sh

# Shift past argv[0]
shift 1

for file in "$@"
do
        svn log $file
done

You could name it 'multilog' or something like that. Call it like this:

./multilog.sh foo.c abc.php bar.h Makefile

It allows for a little more sanity when being called by automated build scripts, i.e. test the existence of each before talking to SVN, or redirect each output to a separate file, insert it into a sqlite database, etc.

That may or may not be what you are looking for.

Tim Post