-2

If array = ["stack","overflow","5","6","question","9"]

I want to make another array to store all numerical values like: new_arr = [5,6,9] in bash.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • From where comes this array please ? – Gilles Quénot Dec 27 '22 at 17:39
  • if array of string is given how can we extract integer from it using bash – KAVITA SINGH Dec 27 '22 at 17:44
  • 4
    There are no commas or square brackets in bash array. Can you show actual code? – anubhava Dec 27 '22 at 17:44
  • what you've provided in the question is invalid `bash` ... invalid assigment statement (no spaces on either side of `=`) ... invalid array syntax (no brackets, no commas); if your initial array is named `array` then please update the question to show the complete output from `typeset -p array` – markp-fuso Dec 27 '22 at 18:18
  • 1
    please update the question to show the (`bash`) code you've tried so far and the (wrong) output generated by your code – markp-fuso Dec 27 '22 at 18:18
  • 1
    @KAVITASINGH Do you have any plan of updating your question to show the real code with the array of strings? – Ted Lyngmo Dec 27 '22 at 23:14

4 Answers4

1

This is how you can do in bash:

# declare original array
arr=("stack" "overflow" "5" "6" "question" "9")

# remove all elements that have at least one non-digit character
narr=(${arr[@]//*[!0-9]*})

# check content of narr
declare -p narr

Output:

declare -a narr='([0]="5" [1]="6" [2]="9")'
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

In pure bash with comments inline:

#!/bin/bash

# the initial array
array=( "stack" "overflow" "5" "6" "question" "9" )

# declare the final array
declare -a new_arr

# loop over the original values
for val in "${array[@]}"
do
    # use regex to filter out those with only digits
    if [[ $val =~ ^[0-9]+$ ]];
    then
        # and append them to new_arr
        new_arr+=($val)
    fi
done

# print the result
for val in "${new_arr[@]}"
do
    echo "$val"
done

If array is a scalar

array='["stack","overflow","5","6","question","9"]'

you should use a parser. is useful for generic stream editing and is a parser specifically made to deal with JSON data.

Here jq used to select only those values that contain at least 1 character and where all characters must be numbers using the ^[0-9]+$:

readarray -t new_arr < <(echo "$array" | jq -r '.[] | select(test("^[0-9]+$"))')
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • This is not the user input – Gilles Quénot Dec 27 '22 at 17:57
  • 1
    @GillesQuenot I'm sorry, but I don't understand. Doesn't the array I created contain the same elements as OP showed? Edit: Aha, you mean OP misrepresented it as a scalar? – Ted Lyngmo Dec 27 '22 at 17:58
  • @GillesQuenot Fair enough. :-) I see that question has already been raised under the question. If the input is indeed what OP shown and just not a mistake, I'll amend my answer. – Ted Lyngmo Dec 27 '22 at 18:01
  • 1
    @GillesQuenot It seems OP abandoned this without clarifying it further, but I added a `jq` version to the answer. – Ted Lyngmo Feb 10 '23 at 10:28
0

With a for loop you will be able to go through your array and then, get inspiration with this post to determine if your iterator's value is a number or a string.

In case of a numerical value, just append it to another array.

arnaudm
  • 157
  • 1
  • 10
  • 3
    This should belongs as a comment – Gilles Quénot Dec 27 '22 at 18:02
  • I think this is borderline. Technically it can work as an answer to the question "_How to extract numerical value from array of string in bash_" because it explains what needs to be done in words, but I really doubt it will be helpful to the asker without following the link. I assume the asker wanted code. – starball Dec 27 '22 at 20:06
  • 1
    Rules are clear on so & co, you can provide links if you copy code. Links could be dead... – Gilles Quénot Dec 27 '22 at 22:26
-1

With :

The proper tool for your proper input:

array = ["stack","overflow","5","6","question","9"]
#!/bin/bash

arr=( $(
    node<<EOF
    new_arr = ["stack","overflow","5","6","question","9"]
    .filter(e => e.match(/^\d+$/));
    new_arr.forEach(e => console.log(e))
EOF
)) 
printf '%s\n' "${arr[@]}" # plain bash array

Online test

new_arr = ["stack","overflow","5","6","question","9"]
.filter(e => e.match(/^\d+$/));
new_arr.forEach(e => console.log(e))
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223