'AWK return/print as associative arrays in Bash

With this command I am trying to filter through my firewall. In order to extract the firewall rule ID number and the IP address.

existing=($(ufw status numbered | grep -e ''"$ssh_port"'' | grep -i 'SSH' | awk '{gsub(/[][]/,""); print $1 $5}'))

The raw output from ufw status numbered looks like this:

[286] 22                        ALLOW IN    1.1.1.1                    # SSH
[287] 22                        ALLOW IN    1.1.1.1                    # SSH
[299] 22                        ALLOW IN    aaaa:aaaa:aaaa:aaaa::aaaa  # SSH

What I am tying to do is return print $1 $5 as an array.

In order to access each line in bash like this:

echo ${existing[0][0]} -> 286
echo ${existing[0][1]} -> 1.1.1.1

echo ${existing[1][0]} -> 287
echo ${existing[1][1]} -> 1.1.1.1

echo ${existing[2][0]} -> 299
echo ${existing[2][1]} -> aaaa:aaaa:aaaa:aaaa::aaaa

How can I achieve this?



Solution 1:[1]

Simulating the ufx output:

$ cat ufw.dat
[286] 22                        ALLOW IN    1.1.1.1                    # SSH
[287] 22                        ALLOW IN    1.1.1.1                    # SSH
[299] 22                        ALLOW IN    aaaa:aaaa:aaaa:aaaa::aaaa  # SSH

One idea where we let awk pass the desired data to a bash/while loop for parsing and storage in a pair of arrays:

declare -a id_array=()
declare -a ip_array=()

ndx=-1

while read -r id ip
do
        (( ndx++ ))
        id_array[$ndx]="${id}"
        ip_array[$ndx]="${ip}"

done < <(cat ufw.dat | awk '/SSH/ {gsub(/[][]/,""); print $1,$5}')

This generates:

$ declare -p id_array ip_array
declare -a id_array=([0]="286" [1]="287" [2]="299")
declare -a ip_array=([0]="1.1.1.1" [1]="1.1.1.1" [2]="aaaa:aaaa:aaaa:aaaa::aaaa")

OP can now use the common index to access both components at the same time, eg,

for i in "${!id_array[@]}"
do
    echo "$i : ${id_array[$i]} : ${ip_array[$i]}"
done

0 : 286 : 1.1.1.1
1 : 287 : 1.1.1.1
2 : 299 : aaaa:aaaa:aaaa:aaaa::aaaa

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 markp-fuso