Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 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带着礼物跑
echo "4 3 2 1" | awk -f sortNum.awk
1 4 3 2 #### 4 1 2 3
我正在使用 GNU Awk 版本 3.1.8。
for (i in a)不按数字顺序选择索引,您需要明确执行此操作。
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]; }