I need help with checking a number next to a word from an array, here is the output from the array:
declare -a my_array=([0]="user.name 16" [1]="user.name2 11" [2]="user.name3 7" [3]="user.name 2"
as you can see, after each username there is a number, what cycle can be used to check exactly (of course, in each element of the array, there can be either 1 or 100) this number (less than 20) and write the username and the original number in the output?
CodePudding user response:
Putting on my mind-reading hat: you can do
for user in "${my_array[@]}"; do
IFS=" " read -r name number <<< "$user"
# do something with $name and $number
CodePudding user response:
You can cut the string based on " " and get the number field. For instance:
for I in "${my_array[@]}"; do
SOME_VAR=$(echo $I | cut -d " " -f 2)
...
<do something with SOME_VAR>
...
# Write username and number to the output
echo $I
done
This will produce the desired output, by choosing the 2nd field by cutting the string on space ' '.