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
.
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
.
4
Answers
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.
With nodejs:
The proper tool for your proper input:
Online test
This is how you can do in
bash
:Output:
In pure bash with comments inline:
If
array
is a scalaryou should use a parser. sed is useful for generic stream editing and jq 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 regex^[0-9]+$
: