Question: I have a log line that I am pulling from bash like:

```
There are 5 apples and 7 oranges

```

And I want to get the 5 and 7 into bash variables and am having problems doing so.

awk '{ print $3, $6 }' won't work due to the fact that their position might change, if there are bananas. Bonus points if the digits can be associated with their fruits.

Thanks in advance

Answer: 
A bash-only method, tested with bash v4:

```
str=" There are 5 apples, 12 bananas and 7 oranges"

i=0
while [[ "$str" =~ ([0-9]+)" "([a-z]+)(.*) ]] ; do
    num[$i]=${BASH_REMATCH[1]}
    type[$i]=${BASH_REMATCH[2]}
    str=${BASH_REMATCH[3]}
    (( i++ ))
done

for (( i=0; i<${#num[@]}; i++ )); do
    printf "%4d\t%s\t%s\n" $i ${num[$i]} ${type[$i]}
done

```

outputs

```
   0    5       apples
   1    12      bananas
   2    7       oranges

```
Grep will do this:

```
echo 'There are 5 apples and 7 oranges' | grep -o -E '[[:digit:]]+'

```