加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash按字母顺序排序字符串列表

发布时间:2020-12-16 01:11:15 所属栏目:安全 来源:网络整理
导读:我有一个文件列表,其中包含我需要排序的版本号 /this/is/a/file/path/product-2.0/file/name/7/this/is/a/file/path/product-2.0/file/name/10/this/is/a/file/path/product-2.0/file/name/12/this/is/a/file/path/product-2.0/file/name/13/this/is/a/file/
我有一个文件列表,其中包含我需要排序的版本号
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9

当我通过grep管道时它就像这样:

echo $files | sort -n
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9

我认为-n被文件名中的第一个数字搞糊涂了.

如何按最后一个数字对数字进行排序

Kaizen ~/so_test $cat ztestfile1 | sort -t'/' -n -k10
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13

这有帮助吗?

替代方式即独立于位置…..

Kaizen ~/so_test  $cat ztestfile1 | sort -V
 /this/is/a/file/path/product-2.0/file/name/6
 /this/is/a/file/path/product-2.0/file/name/7
 /this/is/a/file/path/product-2.0/file/name/8
 /this/is/a/file/path/product-2.0/file/name/9
 /this/is/a/file/path/product-2.0/file/name/10
 /this/is/a/file/path/product-2.0/file/name/12
 /this/is/a/file/path/product-2.0/file/name/13

请注意其排序中的-V(大写)选项,该选项查看字符串中的数字差异以进行排序…..如版本号.

手册页文字::

-V,--version-sort
          natural sort of (version) numbers within text

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读