开发者

Sorting a comma separated list of values

What's the easiest way to sort a comma separated list of values in Mac OS X:

Input: "a, b, aaa, bc"

Output: "a, aaa, b, bc"

开发者_运维问答

I'd like to do this from the terminal so that I can pipe the output to another command.


$ echo "a, b, Aaa, bc" |egrep -o "[^, ]+" |sort -f | xargs |sed -e 's/ /, /g'

if the values contain spaces:

$ echo "a, b, Aaa, bc" |egrep -o "[^, ][^,]*" |sort -f | xargs -I Q echo Q, | xargs

but then you get an extra ", " for free at the end.


echo "a, b, Aaa, bc"|tr -s "[, ]" "\n"|sort|sed -e :a -e 'N;s/\n/,/;ba'

echo "a, b, Aaa, bc"| tr -s "[, ]" "\n"|sort|tr "\n" ","|sed 's/,$//'


echo 'a, b, aaa, bc' | awk '{split($0, a, ", "); n = asort(a); for (i=1; i<=n; i++) {printf a[i]; if (i<n) printf ", "}}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜