4

Is it possible to grep the result of a command spawned by xargs?

As an example I am trying the following command

findbranch prj-xyz -latest|sed 's/^\(.*\/.*\)@@.*$/\1/'|xargs -I {} cleartool lsh {}|grep -m 1 'user'

but seems like grep is executing on the entire result set returned by findbranch, rather individual results of lsh

As an example what I want from above is, for every file returned by findbranch and sed combined I would like to find that version which was last modified by a certain user.

Note If in case it is of a concern, findbranch is an internal utility.

Abhijit
  • 62,056
  • 18
  • 131
  • 204

3 Answers3

2

How about this approach?

.... | xargs -I {} bash -c "cleartool lsh {}|grep -m 1 'user'"

I guess, this answer is self explanatory for you...

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Yes I see your point. I was just wondering if we could have done without spawning a shell for every input line. But seems like this is the only possible option – Abhijit Jan 29 '15 at 12:00
0

Why not use a two phase command? something like

findbranch prj-xyz -latest|sed 's/^\(.*\/.*\)@@.*$/\1/' > /tmp/x ; for x in `cat /tmp/x`; do echo $x; done

Once you see $x is the input you need for xargs you can further manipulate it

e271p314
  • 3,841
  • 7
  • 36
  • 61
0

If you have GNU Parallel this ought to work:

findbranch prj-xyz -latest|sed 's/^\(.*\/.*\)@@.*$/\1/'|parallel cleartool lsh {}'|'grep -m 1 'user'

It will still spawn multiple shells, but at least you can use more CPUs to process it.

Ole Tange
  • 31,768
  • 5
  • 86
  • 104