0

I have the following comand:

grep RJAVA | grep -v grep | wc -l ' | sort | cut -d':' -f2 | cut -d' ' -f2

After executing this, I get the following result :

10 0 0 10

I would like to put all these numbers into a bash array so that I can loop through the array. I tried using xargs but couldn't make it work. Any suggestion ?

Alex
  • 104
  • 1
  • 1
  • 9
  • [Duplicate](http://stackoverflow.com/questions/10586153/bash-split-string-into-array‎)? – hd1 Jun 17 '13 at 20:26
  • 2
    wondering how the `grep ... | grep ... | wc -l` gives you the above result... (wc on pipe results only one number...), so what you sorting? and whats mean the single `'` after the `wc`? strange... – clt60 Jun 17 '13 at 20:50
  • It has a badly paired `'` char. So it is syntactically wrong and it will prompt a '>' to enter the enclosing `'`. But if you remove `'` this waits forever and ever more! First `grep` tries to read something from `stdin`. So this will not write anything to anywhere... Anyway do not use such pipe snakes. Use e.g. one `awk` instead. Or if you have old `awk` use a `sort` and `awk`. – TrueY Jun 17 '13 at 21:06

2 Answers2

3

this should work:

array=($( YOUR_ENTIRE_PIPED_COMMAND ))

BTW, the command above seems broken - you are missing the input to the first grep (either filnames, or pipe into it)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
DVK
  • 126,886
  • 32
  • 213
  • 327
-1

you could try tr:

IN="10 0 0 10"

arr=$(echo $IN | tr " " "\n")

for x in $arr
do
    echo "> [$x]"
done

Regards, Edi