在awk中使用外部关联数组

时间:2015-11-02 12:58:39

标签: arrays bash awk

我有一个创建关联数组的bash脚本。 然后,我启动一个awk命令来解析grep命令的输出。 在我的awk命令中,我想使用关联的数组进行打印,但如果我理解得很好,awk就无法访问它。那我怎么能这样做呢?

我只想列出每个设备(每个设备1个文件),每个端口的速度为“10”,“a-10”,“100”或“a-100”。在输出中,我需要端口(GiX / X / X),VLAN,速度和完整描述

for entry in `ls TF5* | grep -v ".desc"`; do
    declare -A description

    while read line
    do
            if echo $line | grep "Gi" > /dev/null
            then
                    port=$(echo "$line" | awk '{print $1}')
                    description[${port}]=$(echo "$line" | tr -s ' ' | cut -d' ' -f4-)
            fi
    done < $entry.desc

    grep " a-10 " $entry | awk '{printf ("a10:\t%s ;\t%s ;\t%s ;\n", $1, "$description[$1]", $4)}'
    ...

输入样本: 文件TF5XXXXXX没有.desc

Port      Name               Status       Vlan       Duplex  Speed Type
Gi1/0/1   *** SW1-BTB-XXX connected    trunk      a-full a-1000 10/100/1000BaseTX
Gi1/0/4   XYZAB connected    trunk      a-full a-100 10/100/1000BaseTX
Gi1/0/5   YAZER connected    trunk      a-full 10 10/100/1000BaseTX
Gi1/0/6   BLABL connected    trunk      a-full a-10 10/100/1000BaseTX
Gi1/0/7   AHAHO connected    trunk      a-full 1000 10/100/1000BaseTX
Gi1/0/8   XXXXX connected    trunk      a-full 100 10/100/1000BaseTX
Gi1/0/9   XXXXX connected    trunk      a-full a-1000 10/100/1000BaseTX

使用.desc文件TF5XXXXXX

Interface                      Status         Protocol Description
Vl1                            admin down     down
Vl101                          up             up
Fa0                            down           down
Gi1/0/1                        up             up       *** SW1-BTB-BALBLA ***
Gi1/0/2                        down           down
Gi1/0/3                        down           down     DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/4                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/5                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/6                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/7                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/8                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT
Gi1/0/9                        up             up       DESCRIPTION CENSURED BUT I NEED TO CATCH IT

输出样本:

a-100 TAB Gi1/0/4 TAB  DESCRIPTION TAB VLAN
10 TAB Gi1/0/5 TAB  DESCRIPTION TAB VLAN
a-10 TAB Gi1/0/6 TAB  DESCRIPTION TAB VLAN
100 TAB Gi1/0/8 TAB  DESCRIPTION TAB VLAN

感谢您的时间:)

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

          name
1 TOURDEFRANCE
2 TOURDEFRANCE
3 TOURDEFRANCE
4 TOURDEFRANCE
5    GET FRESH

结果:

for entry in TF5*.desc;do 
  awk 'NR==FNR{key=$1;$1=$2=$3="";a[key]=$0;next}
       $(NF-1)~/(a-)?10(0)?$/{print $(NF-1)"\t"$1"\t"a[$1]"\t"$4}' $entry ${entry%.desc}
done