Alex
Alex

Reputation: 104

I want to insert/store wc -l result into a bash array

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 ?

Upvotes: 0

Views: 452

Answers (2)

DVK
DVK

Reputation: 129481

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)

Upvotes: 3

Eduard Iten
Eduard Iten

Reputation: 11

you could try tr:

IN="10 0 0 10"

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

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

Regards, Edi

Upvotes: -1

Related Questions