GNU awk
ここにワンライナーがある場合:
$ gawk '{s[NR]=$1;c[NR]=$2 $3}END{for(i=0;++i<=asort(s);)print s[i] c[i]}' file
1,45,9
2,47,6
3,46,7
4,48,4
5,10,5
6,11,1
そうでない場合は、awk
単純なバブル ソートを実装するスクリプトを次に示します。
{ # read col1 in sort array, read others in col array
sort[NR] = $1
cols[NR] = $2 $3
}
END { # sort it with bubble sort
do {
haschanged = 0
for(i=1; i < NR; i++) {
if ( sort[i] > sort[i+1] ) {
t = sort[i]
sort[i] = sort[i+1]
sort[i+1] = t
haschanged = 1
}
}
} while ( haschanged == 1 )
# print it
for(i=1; i <= NR; i++) {
print sort[i] cols[i]
}
}
ファイルに保存して実行sort.awk
しますawk -f sort.awk file
:
$ awk -f sort.awk file
1,45,9
2,47,6
3,46,7
4,48,4
5,10,5
6,11,1