linux - awk/gawk asorti() 问题

标签 linux unix awk lexicographic asort

我在 gawk 的 asorti 函数中遇到了以下问题:

gawk 'BEGIN{ \

  a[1]=6; \

  a[2]=7; \

  a[3]=8; \

  a[21]=9; \

  a[123]=10; \

  t=asorti(a, o); \

  for (i=1; i<=t; i++) { \

    print i,o[i]; \

  } \

}'

结果是:

1 1

2 123

3 2

4 21

5 3

所以很清楚 awk,按字典顺序对索引进行排序,但是 awk 的文档说 (asort(s[, d])):

"The contents of s are sorted using gawk’s normal rules for comparing values..."

但是,当我将索引本身复制到临时数组中,并使用 asort() 对该新数组进行排序时,

看起来没问题(使用类似的东西):

j=1; for(e in a) { temp[j++] = e; }

我是做错了,还是 gawk 的 asorti() 有问题?

最佳答案

根据gawk manual :“要记住关于数组的一个重要方面是数组下标始终是字符串。

关于linux - awk/gawk asorti() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3087031/

相关文章:

linux - 通过 Linux 组织文件

linux - 如何根据行字段为每一行添加前缀

c - inotify_event 返回 wd -1

c++ - 您建议在 Linux 中学习 C++ 编程的书籍或在线资源是什么?

linux - 将自定义 Hook 添加到使用 Docker 安装的 Gitlab

java - 增加 Linux 机器上的堆大小

linux - 管道 Perl 脚本输出到 head -n 10 在打印 10 行后杀死脚本

linux - 如何过滤前九个字符至少包含一个 "x"的行

linux - Unix:遍历一个目录

linux - 在 'find' 命令搜索中插入文本