如何在Linux的特定部分文件名排序? [英] How to sort file names by specific part in linux?

查看:452
本文介绍了如何在Linux的特定部分文件名排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的文件在我的build文件夹中,我试图通过使用sort命令对它们进行排序。

该文件的结构是这样的:

名称 - 版本 - V - 构建日期


  

XXX-2.0.0-快照V2014-07-10_18-01-05.log结果
  XXX-2.0.0-快照V2014-07-10_18-02-05.log结果
  XXX-2.0.0-快照V2014-07-10_18-03-05.log结果
  XXX-2.0.0-快照V2014-07-10_18-04-05.log结果
  XXX-2.0.0-快照V2014-07-10_18-05-05.log


如果我们假设版本字符串将留在3位,对它们进行排序是很容易。如果我添加不同的版本像2.1或2.0.0.2在这里?我需要这样的结果是:


  

XXX-2.1-快照V2014-07-10_18-05-05.log结果
  XXX-2.0.2-快照V2014-07-10_18-04-05.log结果
  XXX-2.0.0.2-快照V2014-07-10_18-03-05.log结果
  XXX-2.0.0.1-快照V2014-07-10_18-02-05.log结果
  XXX-2.0.-快照V2014-07-10_18-01-05.log



解决方案

  $猫文件
XXX-2.0.2-快照V2014-07-10_18-04-05.log
XXX-2.0.0.2-快照V2014-07-10_18-03-05.log
XXX-2.1-快照V2014-07-10_18-05-05.log
XXX-2.0.0.1-快照V2014-07-10_18-02-05.log
XXX-2.0.-快照V2014-07-10_18-01-05.log$排序-V -r -T- -k2,2<文件
XXX-2.1-快照V2014-07-10_18-05-05.log
XXX-2.0.2-快照V2014-07-10_18-04-05.log
XXX-2.0.0.2-快照V2014-07-10_18-03-05.log
XXX-2.0.0.1-快照V2014-07-10_18-02-05.log
XXX-2.0.-快照V2014-07-10_18-01-05.log

注意: 的某些实现排序不支持 -V 选项..

说明:


  • -V:版本排序

  • -T-:分割与列分隔符 -

  • -k2,2:排序场2及只有2

  • -r:反转排序(。根据您的预计产量删除此标志,如果不要求)

I have lots of files in my build folder and I am trying to sort them by using sort command.

The structure of the files are like that:

name - version - 'v' - build date

xxx-2.0.0-SNAPSHOT-V2014-07-10_18-01-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-05-05.log

if we assume that version string will be stay in 3 digit, sorting them is easy. What if I add different versions like 2.1 or 2.0.0.2 here ? I need a result like this:

xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log

解决方案

$ cat file
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log 

$ sort -V -r -t- -k2,2 < file
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log 

Note: Some implementations of sort do not support -V option...

Explanation:

  • -V : Version sort
  • -t- : Split into columns with delimiter '-'
  • -k2,2: Sort by field 2 & only 2
  • -r : reverse sort (based on your expected output. Remove this flag, if not required.)

这篇关于如何在Linux的特定部分文件名排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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