简体   繁体   English

如何在bash中填充多行关联数组

[英]how to fill multiline associative array in bash

I am trying to fill associative array in bash from following example file我正在尝试从以下示例文件中填充 bash 中的关联数组

interface GigabitEthernet1/0/1
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp
interface GigabitEthernet1/0/2
 mls qos trust dscp
interface GigabitEthernet1/0/3
interface GigabitEthernet1/0/4
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp
interface GigabitEthernet1/0/5
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp
interface GigabitEthernet1/0/6
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp
interface GigabitEthernet1/0/7
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp
interface GigabitEthernet1/0/8
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp

interface GigabitEthernet1/0/1 or GigabitEthernet1/0/1 should be the array key and anything between interface sections should be array key value. interface GigabitEthernet1/0/1GigabitEthernet1/0/1应该是数组键,接口部分之间的任何东西都应该是数组键值。

 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp

Expected output should be:预期输出应该是:

 $echo ${array[GigabitEthernet1/0/4]}
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp

There will be multiple different files with similar format, so this need to be done by loop, the text between interfaces should be 0 to 5-6 lines.会有多个格式相似的不同文件,所以这需要循环完成,界面之间的文本应该是0到5-6行。 The reason behind is I need to do logical test on each interface, if it is correct or not.背后的原因是我需要对每个接口进行逻辑测试,如果它正确与否。 Below text is correct, anything else, or empty field is incorrect.下面的文本是正确的,其他任何内容或空白字段都是不正确的。

 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp

So far i have stuck with formatting of IFS and multilines as the value of array key.到目前为止,我一直坚持将 IFS 和多行的格式设置为数组键的值。 I was able to fill non-associative array, but in not the cleanest form.我能够填充非关联数组,但不是最干净的形式。

$ cat test_compare_interface | sed 's/interface.*$/*\n&/g' > test_compare_interface_sed

test_compare_interface is file with interfaces test_compare_interface 是带有接口的文件

$ IFS=$'*' && array=($(cat test_compare_interface_sed))
$ echo ${array[4]}

interface GigabitEthernet1/0/4
 srr-queue bandwidth share 10 10 60 20
 priority-queue out 
 mls qos trust dscp

With associative array loop, i am still strugling使用关联数组循环,我还在挣扎

while IFS='*' read -r interface value; do array[$interface]=$value ; done < test_compare_interface_sed 

Thank you for all the suggestions.谢谢你的所有建议。

Would you please try the following:请您尝试以下操作:

declare -A array

# assign "${val[*]}" to the assciative array "array" keying with $1
store() {
    if [[ -n $1 ]]; then        # will skip the 1st line which is not ready
        array[$1]=$(IFS=$'\n'; echo "${val[*]}")
                                # join "val" with newline
        unset val               # empty the queue
    fi
}

while IFS= read -r line; do
    if [[ $line =~ ^interface ]]; then
        store "$interface"      # assign to the associative array
        interface="$line"       # for the next iteration
    else
        val+=("${line# }")      # append to the queue
    fi
done < file.txt

store "$interface"              # flush the last entry

for i in "${!array[@]}"; do     # print the array elements to test
    echo "$i"
    echo "${array[$i]}"
done
  • It just read the input line by line without modifying IFS .它只是逐行读取输入而不修改IFS
  • If the line starts with "interface", it flushes the queue (array val ) into the associative array and updates the interface name for next iteration.如果该行以“interface”开头,它会将队列(数组val )刷新到关联数组中,并为下一次迭代更新接口名称。
  • If the line does not start with "interface", the line is accumulated in the queue (array val )如果该行不以“interface”开头,则该行在队列中累积(数组val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM