按数字顺序对输出进行排序 bash

流水

我有一个 bash 脚本,它进行多个 API 调用并打印出我需要的特定数据。

它被保存到一个名为“result”的文件中,并使用以下命令将其打印出来。

cat result | column -t -s "|"

这导致:

TYPE           CAPACITY  STATUS     LOCATION
0)ABC          13038G    Allocated  0:0:1 
1)ABC          13038G    Allocated  0:0:0 
2)DEF          13038G    Available  0:0:3 
3)ABC          13038G    Available  0:0:4 
4)GHI          13038G    Available  0:0:5 
5)DEF          13038G    Available  0:0:6 
6)DEF          13038G    Available  0:0:7 
7)ABC          13038G    Available  0:0:8 
8)GGI          13038G    Allocated  0:1:0 
9)BMD          13038G    Available  0:0:2 
10)BMW         1200G     Allocated  6:-1:-1 
11)MER        5588G     Available  0:1:7 
  • 所述0) 1) 2) ... 11)手动添加,它不是数据的一部分。

结果以我想要的方式出现,但我想让它变得更好。

我想以某种方式从 LOCATION 中的值以数字顺序对数据进行排序。

例如,它将像这样列出:

0:0:0
0:0:1
0:0:3
0:1:0
0:1:2
0:1:3
2:0:1
2:1:2

在某种意义上,如果它的 x:y:z

它将检查 z 然后 y 然后 x。

我试过使用 sort 命令,但它只使用前面的 0) 1) 2) 没有帮助......

提前致谢。

拉曼赛洛帕

使用 GNU awk:

awk 'NR==1 { print;next } { split($4,map,":");map1[map[1]][map[2]][map[3]]=$0} END { PROCINFO["sorted_in"]="@ind_num_asc";for ( i in map1 ) { for ( j in map1[i]) { for ( k in map1[i][j]) { print map1[i][j][k] } } } }' file

说明:

awk 'NR==1 { 
             print;                                             # For the first line ( headers) print and skip to the next line
             next 
           } 
           { split($4,map,":");                                 # Split the fourth space separated field into the array map using ":" as the delimiter
             map1[map[1]][map[2]][map[3]]=$0                    # Create a 3 dimensional array called map with the first, second and third indexes of map as the indexes and the line as the value
           } 
       END { 
             PROCINFO["sorted_in"]="@ind_num_asc";              # Set the array ordering (index number ascending)
             for ( i in map1 ) { 
               for ( j in map1[i]) { 
                  for ( k in map1[i][j]) { 
                    print map1[i][j][k]                          # At the end of processing the file, loop through the map1 array and print the values.
                  } 
                } 
              } 
             }' file

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

按从特定列中提取的数字对bash输出进行排序

在 Bash 中,如何对 JQ 的 Echo 输出进行排序?

MongoDB按字母顺序对输出进行排序(分组后)

外壳按字母顺序对输出进行排序

按添加的WHERE条件的顺序对SQL输出进行排序

如何遍历字典列表并根据 Bash 中的键对输出进行排序?

在Bash中对文件/文件夹输出进行排序

按字段按字母顺序对描述表foo输出进行排序

按bash按字典顺序排序

Python-如何不按字母顺序对Sphinx输出进行排序

如何按自定义顺序对T-SQL查询输出进行排序?

如何按最新的修改日期按时间顺序对“ grep -l”的输出进行排序?

如何在bash中对列表中的输出进行编号?

按名称对文件进行数字排序,然后按BASH中的父目录排序

按字母顺序排序bash参数

使用bash通配符按数字排序

按文件大小对grep的输出进行排序?(重击)

按文件大小对grep的输出进行排序?(重击)

如何按日期对SVN LS -R输出进行排序

按日期对csv文件的输出进行排序python

按数字对文件进行排序并在 Bash 中的条目之间保留空行

对“查找”的输出进行排序?

将正则表达式与grep输出进行比较-bash脚本

我可以在bash脚本中与OSX`say`命令的输出进行交互吗?

按数字顺序对变量内容进行排序

按数字顺序对文件进行排序

按数字顺序对列表进行排序

.bash_history内容按字母顺序排序

在bash中的多列表上按从右到左的顺序排序