AyeitzaMoleRat
AyeitzaMoleRat

Reputation: 49

Two Arrays from one text file

I have been completely stumped by this one. I have a piece of code that spits out this...

192.168.0.1=12345
192.168.0.2=35345
192.168.0.3=72345
192.168.0.4=43345

That is written to a text file then loaded back into a program into an array.

Is there a way to split it into 2D array? The first D containing the IP address the second the other numbers. I will need to find out the IP that is linked to the numbers later in the code.

So far I just have this...

IFS=$'\r\n' GLOBIGNORE='*' command eval  'uparray=($(cat ./uptime.txt))'

I should probably mention this is running on Raspbian

Upvotes: 0

Views: 54

Answers (1)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19335

if your bash version supports associative array

declare -A ip_nums
while IFS== read ip num; do
    ip_nums[$num]=$ip
done <./uptime.txt

then to retreive ip from num

echo "${ip_nums[$num]}"

EDIT: To memorize the biggest number in the loop

biggest=0
while ...
    ...
    if ((num>biggest)); then
        biggest=$num
    fi
done ...

Upvotes: 1

Related Questions