我會添加一個gawk解決方案重刑你的長長的選項列表。
這是一個獨立的腳本:
#!/usr/bin/env gawk -f
{
line=$1
# Collect the tuples into values of an array,
for (i=2;i<NF;i+=2) a[i]=$i FS $(i+1)
# This sorts the array "a" by value, numerically, ascending...
asort(a, a, "@val_num_asc")
# And this for loop gathers the result.
for (i=0; i<length(a); i++) line=line FS a[i]
# Finally, print the line,
print line
# and clear the array for the next round.
delete a
}
這是通過複製你的元組到一個數組,排序數組,然後重裝排序元組中一個for循環,打印數組元素。
請注意,由於使用了asort()
,所以它只是gawk(不是傳統的awk)。
$ cat infile
string 2 2 3 3 1 4
other 5 1 20 9 3 7
$ ./sorttuples infile
string 1 4 2 2 3 3
other 3 7 5 1 20 9
首先,你不能排序一行。嘗試循環訪問數據並將其放入基於奇數的數組中(假設您是正確的並且它們是唯一的) – grail