AWK数组以意外顺序打印元素 [英] Awk array is printing elements in unexpected order

查看:67
本文介绍了AWK数组以意外顺序打印元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用awk从数据集中捕获时间戳,并打印出与该时间戳关联的值(内存).

I am using awk to capture timestamp from a dataset, and print out a value (memory) associated with that timestamp.

以下awk代码可以很好地实现这一目标.

The following awk code works well to achieve this.

awk '
/show memory compare start/     {getline
                                 start_time = $0;
                                }
/show memory compare end/       {getline
                                 end_time = $0;
                                }
/mibd_interface/{
        print start_time, $3, "\n" end_time, $4
}' snmpoutput.txt

Thu Sep 19 14:38:06.400 WIB 8670334
Thu Sep 19 14:40:56.123 WIB 8484152
Thu Sep 19 14:43:07.946 WIB 8369050
Thu Sep 19 14:45:27.916 WIB 8514825
Thu Sep 19 14:46:28.464 WIB 8446906
Thu Sep 19 14:50:10.422 WIB 8264885
Thu Sep 19 14:50:44.374 WIB 8264884
Thu Sep 19 14:55:05.760 WIB 8264960

将这些数据放入数组并打印后,元素显示为乱序.

After putting this data into an array and printing it, the elements appear out of order.

在将该输出与上面所需的输出进行比较时,我已经在最右边的列中输入了显示顺序.

I have entered the order of appearance in the right most column, when comparing this output with the desired output above.

awk '
/show memory compare start/     {getline
                                 start_time = $0;
                                }
/show memory compare end/       {getline
                                 end_time = $0;
                                }

/mibd_interface/{mem_stats[start_time]=$3; mem_stats[end_time]=$4} END {for (time in mem_stats) {printf "%s => %s\n",time,mem_stats[time]}}' snmpoutput.txt

Thu Sep 19 14:55:05.760 WIB => 8264960  8
Thu Sep 19 14:45:27.916 WIB => 8514825  4 
Thu Sep 19 14:43:07.946 WIB => 8369050  3
Thu Sep 19 14:40:56.123 WIB => 8484152  2 
Thu Sep 19 14:50:44.374 WIB => 8264884  7
Thu Sep 19 14:38:06.400 WIB => 8670334  1  
Thu Sep 19 14:50:10.422 WIB => 8264885  6
Thu Sep 19 14:46:28.464 WIB => 8446906  5

数据集

(由于完整的数据集太大,因此发布了样本)

DATASET

(posted a sample as full dataset is too large)

一次迭代

xr_lab#show memory compare start
Thu Sep 19 14:38:06.400 WIB   
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_start.out
xr_lab#
xr_lab#
xr_lab#show memory compare end
Thu Sep 19 14:40:56.123 WIB   
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_end.out
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:41:08.084 WIB

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------

2550   sysdb_svr_local          7881443     7878256     -3187       87391
7582   mibd_interface           8670334     8484152     -186182     267657

第二次迭代

xr_lab#show memory compare start
Thu Sep 19 14:43:07.946 WIB   
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_start.out
xr_lab#
xr_lab#
xr_lab#
xr_lab#show memory compare end
Thu Sep 19 14:45:27.916 WIB   
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_end.out
xr_lab#
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:45:42.091 WIB

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
6777   ospf                     24294569    24283592    -10977      227389
7582   mibd_interface           8369050     8514825     145775      126259

我可以知道为什么这些元素打印不正确吗,以及解决此问题的最佳方法吗?

Can I know why the elements are printed out of order, and the best way to fix this?

谢谢.

推荐答案

我可以知道为什么这些元素打印不正确吗,以及解决此问题的最佳方法吗?

Can I know why the elements are printed out of order, and the best way to fix this?

标准具有以下含义:

awk语言提供了用于存储数字或字符串的数组.不需要声明数组.它们最初应为空,其大小应动态更改.下标或元素标识符是字符串,提供了类型的关联数组功能.< snip>

The awk language supplies arrays that are used for storing numbers or strings. Arrays need not be declared. They shall initially be empty, and their sizes shall change dynamically. The subscripts, or element identifiers, are strings, providing a type of associative array capability. <snip>

for (variable in array)

将进行迭代,以未指定的顺序将数组的每个索引分配给变量.

which shall iterate, assigning each index of the array to variable in an unspecified order.

因此,我们知道awk中的数组是关联数组,没什么不仅仅是键值对组合.编程世界中的经典示例是二进制树,例如C ++的 std :: map .通常,需要强加排序以有效遍历和搜索数组,但是标准awk并没有给我们提供定义这种排序的选项.该标准使密钥顺序对于实施awk的任何人都可以自由选择.这就是为什么它声明 for(数组中的变量)将以未指定的顺序遍历数组的原因.

So from this, we know that an array in awk is an associative array, nothing more than a key-value-pair combination. A classic example in the programming world is a binary-tree such as C++'s std::map. Usually, ordering needs to be imposed to traverse and search the array efficiently, however standard awk does not give us the option to define such ordering. The standard leaves the key-order a free choice for whoever implements awk. That is also why it states that for (variable in array) will traverse the array in an unspecified order.

GNU awk允许使用数组变量 并在本地一级使用

GNU awk, on the other hand, allows one to define the key-order on a global level using the array-variable PROCINFO["sorted_in"] and on a local level, using the asorti(source [, dest [, how ] ]) function. The latter will store the keys of array source in an integer-indexed array dest. The latter is populated such that the order of the keys is defined by the function how (dest[1] < dest[2] < dest[3] < ... with how defining <).

如果您不想使用GNU awk功能,并且知道已对输入进行了排序,则可以使用两个数组.一种跟踪键顺序,另一种跟踪键值:

If you do not want to use GNU awk features and you know you have sorted input, then you can make use of two arrays. One that keeps track of the key-order and one that keeps track of the key-values:

{ key_order[++c]="key"
  data["key"] = "value" }
END { for(i=1;i<=c;++i) print data[key_order[i]] }

这篇关于AWK数组以意外顺序打印元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆