我有一个 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] 删除。
我来说两句