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

前端之家收集整理的这篇文章主要介绍了bash按字母顺序排序字符串列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个文件列表,其中包含我需要排序的版本号
  1. /this/is/a/file/path/product-2.0/file/name/7
  2. /this/is/a/file/path/product-2.0/file/name/10
  3. /this/is/a/file/path/product-2.0/file/name/12
  4. /this/is/a/file/path/product-2.0/file/name/13
  5. /this/is/a/file/path/product-2.0/file/name/6
  6. /this/is/a/file/path/product-2.0/file/name/8
  7. /this/is/a/file/path/product-2.0/file/name/9

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

  1. echo $files | sort -n
  2. /this/is/a/file/path/product-2.0/file/name/10
  3. /this/is/a/file/path/product-2.0/file/name/12
  4. /this/is/a/file/path/product-2.0/file/name/13
  5. /this/is/a/file/path/product-2.0/file/name/6
  6. /this/is/a/file/path/product-2.0/file/name/7
  7. /this/is/a/file/path/product-2.0/file/name/8
  8. /this/is/a/file/path/product-2.0/file/name/9

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

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

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

这有帮助吗?

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

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

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

手册页文字::

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

猜你在找的Bash相关文章