Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对关联数组bash脚本进行排序_Bash_Sorting_Associative - Fatal编程技术网

如何对关联数组bash脚本进行排序

如何对关联数组bash脚本进行排序,bash,sorting,associative,Bash,Sorting,Associative,如何在bash中对关联数组排序 例如,我在bash中使用了数组: [0,0]="Max" [0,1]="25" [1,0]="Vladimir" [1,1]="0" [2,0]="Mayki" [2,1]="50" 输出必须是: 梅基-50 最多25 弗拉基米尔-0 我不知道这个数组如何排序 其他信息:我从文本文件(“log.txt”)解析assoc数组 #/bin/bash 声明-A b_数组 #读取参数中的文件并填充名为“array”的数组 getArray(){ i=0 w=9 计数=1

如何在bash中对关联数组排序

例如,我在bash中使用了数组:

[0,0]="Max"
[0,1]="25"
[1,0]="Vladimir"
[1,1]="0"
[2,0]="Mayki"
[2,1]="50"
输出必须是:

  • 梅基-50
  • 最多25
  • 弗拉基米尔-0
  • 我不知道这个数组如何排序

    其他信息:我从文本文件(“log.txt”)解析assoc数组

    #/bin/bash
    声明-A b_数组
    #读取参数中的文件并填充名为“array”的数组
    getArray(){
    i=0
    w=9
    计数=10
    边读边读
    做
    k=0
    #数组[i]=$line#将其放入数组中
    
    #b_array[$i,0]=$(grep-Eo)(http | https)://[a-zA-Z0-9./?=-]*“对于如此复杂的任务,我可能会切换到Perl,即使它在bash中仍然可行:

    #!/bin/bash
    
    declare -A arr
    arr=([0,0]="Max"
         [0,1]="25"
         [1,0]="Vladimir"
         [1,1]="0"
         [2,0]="Mayki"
         [2,1]="50"
         [10,0]=Ivan
         [10,1]=10
        )
    
    indices=( $( (IFS=$'\n' ; echo "${!arr[*]}") | grep ,0 | cut -d, -f1 | sort ) )
    
    for i in "${indices[@]}" ; do
        echo ${arr[$i,0]} ${arr[$i,1]}
    done | sort -rnk2
    
    如果将数组定义为

    arr=([Max]=25
         [Vladimir]=0
         [Mayki]=50
         [Ivan]=10
        )
    
    for paren in "${!arr[@]}" ; do
        echo $paren ${arr[$paren]}
    done | sort -rnk2
    

    解决此问题有两种方法。最简单的方法之一可能是将字符串对读入索引数组,然后对数字字段进行反向数字排序:

    #!/bin/bash
    
    declare -A arr
    declare -a sa
    
    arr[0,0]="Max"
    arr[0,1]="25"
    arr[1,0]="Vladimir"
    arr[1,1]="0"
    arr[2,0]="Mayki"
    arr[2,1]="50"
    
    ## convert associative array to 
    #  indexed array of string pairs
    #  (e.g. "Max - 25", "Mayki - 50" )
    for i in ${!arr[@]}; do                         # for each key in ar
    
        x=${i%,*}                                   # separate x,y values
        y=${i#*,}
        (( y == 1 )) && continue                    # if y == 1, continue
    
        for j in ${!arr[@]}; do                     # for each key in ar
    
            _x=${j%,*}                              # separate _x,_y values
            _y=${j#*,}
            ((x != _x)) || ((_y == 0)) && continue  # if x != _x, or _y == 0, continue
    
            sa+=( "${arr[$i]} - ${arr[$j]}" )       # add combined string to indexed sa
    
        done
    done
    
    sort -r -k3 -n <<<"$(printf "%s\n" "${sa[@]}")" # numeric reverse sort sa on key3
    
    exit 0
    

    您最好使用具有更好数据结构支持的语言。输入实际上是什么样子的?该示例数组与什么有什么关系?您没有创建这样的数组(bash无法做到)。您在输入中似乎没有这样的数组(尽管这一点还不清楚).关联数组没有顺序,所以最好的方法是提取并排序键,然后循环遍历已排序的键列表。这是我在KPI大学的家庭作业最初的任务:我需要解析文件并按字节数显示10行查询。使用关联数组的方法非常奇怪。通常使用e name作为键:
    ([Max]=25[Mayki]=50[Vladimir]=0)
    我有错误::找不到命令:不是有效标识符:
    arr:command-not-found:command-not-found'est3.sh:line 16:意外标记附近的语法错误
    do'est3.sh:line 16:`for“${index[@}”中的I;do@МааааааааааБаа:bash的哪个版本?@МааааБааааааааа107-(什么错误?我有
    GNU bash,版本4.3.33
    如果这给您带来了问题,还有其他方法可以对索引数组进行排序。我检查了,也在
    GNU bash,版本4.2.37
    上工作(我有):无此类文件或目录H:command not found':无效标识符
    ar]:无效标识符
    sa:command not found:command not found sort.sh:第36行:语法错误:意外的文件结尾-bash-4.1$What?是否确实复制/粘贴了它?让我们将
    ar
    更改为
    arr
    以避免与e冲突xternal
    ar
    命令(无论如何它不应该这么做)立即尝试。抱歉,请检查服务器控制台VPS(4.1)上的代码。它仍然在虚拟机上,将尝试运行(4.3)。好的,它工作得很好。我已经在多台机器上运行过(Archlinux和openSuSE)要确认,
    ar':不是有效标识符
    sa:command not found:command not found sort.sh:line 36
    错误毫无意义。如果您遇到这些错误,您的shell解释器可能有问题。请告诉我您发现了什么。
    #!/bin/bash
    
    declare -A arr
    declare -a sa
    
    arr[0,0]="Max"
    arr[0,1]="25"
    arr[1,0]="Vladimir"
    arr[1,1]="0"
    arr[2,0]="Mayki"
    arr[2,1]="50"
    
    ## convert associative array to 
    #  indexed array of string pairs
    #  (e.g. "Max - 25", "Mayki - 50" )
    for i in ${!arr[@]}; do                         # for each key in ar
    
        x=${i%,*}                                   # separate x,y values
        y=${i#*,}
        (( y == 1 )) && continue                    # if y == 1, continue
    
        for j in ${!arr[@]}; do                     # for each key in ar
    
            _x=${j%,*}                              # separate _x,_y values
            _y=${j#*,}
            ((x != _x)) || ((_y == 0)) && continue  # if x != _x, or _y == 0, continue
    
            sa+=( "${arr[$i]} - ${arr[$j]}" )       # add combined string to indexed sa
    
        done
    done
    
    sort -r -k3 -n <<<"$(printf "%s\n" "${sa[@]}")" # numeric reverse sort sa on key3
    
    exit 0
    
    $ bash sort_assoc.sh
    Mayki - 50
    Max - 25
    Vladimir - 0