如何在bash中对逗号分隔的值进行排序? [英] How to sort comma separated values in bash?

查看:130
本文介绍了如何在bash中对逗号分隔的值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数字

7, 15, 6, 2, -9

我想在bash中这样排序(从命令行或脚本文件)

I want to sort it like this in bash(from command line or either a script file)

-9, 2, 6, 7, 15

我该怎么做?我无法使用sort命令获得此信息.

How can I do this? I am unable to get this using sort command.

推荐答案

echo "7, 15, 6, 2, -9" | sed -e $'s/,/\\\n/g' | sort -n | tr '\n' ',' | sed 's/.$//'

  1. sed -e $'s/,/\\\n/g':用于通过逗号将字符串分成几行.
  2. sort -n:然后您可以使用按数字排序
  3. tr '\n' ',':将换行符分隔为逗号.
  4. sed 's/.$//':删除尾巴逗号.
  1. sed -e $'s/,/\\\n/g': For splitting string into lines by comma.
  2. sort -n: Then you can use sort by number
  3. tr '\n' ',': Covert newline separator back to comma.
  4. sed 's/.$//': Removing tailing comma.

不够优雅,但应该可以:p

Not elegant enough, but it should work :p

这篇关于如何在bash中对逗号分隔的值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆