如何在awk中对数组进行排序?考虑“sortNum.awk”:
{
split($0,a," ")
for (i in a) print a[i]
print "####"
asort(a)
for (i in a) print a[i]
}
使用echo "4 3 2 1" | awk -f sortNum.awk
运行
1
4
3
2
####
4
1
2
3
我正在使用GNU Awk版本3.1.8。
答案 0 :(得分:5)
for (i in a)
不按数字顺序选择索引,您需要明确地执行此操作。
{
n = split($0,a," ");
for (i = 1; i <= n; i++) print a[i];
print "####"
asort(a)
for (i = 1; i <= n; i++) print a[i];
}