如何从linux命令行数字排序文件 [英] How to sort files numerically from linux command line

查看:187
本文介绍了如何从linux命令行数字排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,现在这是一个关于Linux比一个问题,但也许有人知道如何做我想要的。我知道这可以 使用 sort 命令实现,但我想要一个更好的解决方案,因为让它工作就像写一个C程序做同样的事一样简单。

Okay, now this is more a rant about Linux than a question, but maybe someone knows how to do what I want. I know this can be achieved using the sort command, but I want a better solution because getting that to work is about as easy as writing a C program to do the same thing.

我有文件,为了参数,让我说我有这些文件:(我的文件是相同的我只有更多)

I have files, for arguments sake, lets say I have these files: (my files are the same I just have many more)


  • file-10.xml

  • file-20.xml

  • file-100.xml

  • file-k10.xml

  • file-k20.xml

  • file-k100.xml

  • file-M10.xml

  • file-M20.xml

  • file-M100.xml

  • file-10.xml
  • file-20.xml
  • file-100.xml
  • file-k10.xml
  • file-k20.xml
  • file-k100.xml
  • file-M10.xml
  • file-M20.xml
  • file-M100.xml

现在这就是我想要排序的顺序。这是Windows中的顺序 ,它们默认排序为。那很好。

Now this turns out to be the order I want them sorted in. Incidentally, this is the order in Windows that they are by default sorted into. That's nice. Windows groups consecutive numerical characters into one effective character which sorts alphabetically before letters.

如果我键入 ls ,则会将字母顺序排在字母之前。 c $ c>在linux命令行,我得到以下垃圾。注意20被移位。这是一个更大的交易,当我有数百个这些文件,我想在报告中按顺序查看。

If I type ls at the linux command line, I get the following garbage. Notice the 20 is displaced. This is a bigger deal when I have hundreds of these files that I want to view in a report, in order.


  • file-100。 xml

  • file-10.xml

  • file-20.xml

  • file-k100.xml

  • file-k10.xml

  • file-k20.xml

  • file-M100.xml

  • file-M10.xml

  • file-M20.xml

  • file-100.xml
  • file-10.xml
  • file-20.xml
  • file-k100.xml
  • file-k10.xml
  • file-k20.xml
  • file-M100.xml
  • file-M10.xml
  • file-M20.xml

我可以使用 ls -1 | sort -n -k 1.6 以获取没有'k'或'M'正确的那些...

I can use ls -1 | sort -n -k 1.6 to get the ones without 'k' or 'M' correct...


  • file-k100.xml

  • file-k10.xml

  • file-k20.xml

  • -M100.xml

  • file-M10.xml

  • file-M20.xml

  • file-10 .xml

  • file-20.xml

  • file-100.xml

  • file-k100.xml
  • file-k10.xml
  • file-k20.xml
  • file-M100.xml
  • file-M10.xml
  • file-M20.xml
  • file-10.xml
  • file-20.xml
  • file-100.xml

我可以使用 ls -1 | sort -n -k 1.7 无法获取任何内容

I can use ls -1 | sort -n -k 1.7 to get none of it correct



  • file-10.xml

  • file-20.xml

  • file-k10.xml

  • file-M10.xml

  • file-k20.xml

  • file-M20.xml

  • file-k100.xml

  • file-M100.xml

  • file-100.xml
  • file-10.xml
  • file-20.xml
  • file-k10.xml
  • file-M10.xml
  • file-k20.xml
  • file-M20.xml
  • file-k100.xml
  • file-M100.xml

。让我们真正做到正确。 ls -1 | grepfile- [0-9] * \.xml| sort -n -k1.6&& ls -1 file-k * .xml | sort -n -k1.7&&& ls -1 file-M * .xml | sort -n -k1.7

Okay, fine. Let's really get it right. ls -1 | grep "file-[0-9]*\.xml" | sort -n -k1.6 && ls -1 file-k*.xml | sort -n -k1.7 && ls -1 file-M*.xml | sort -n -k1.7


  • file-10.xml

  • file-20.xml

  • file-100.xml

  • file-k10.xml

  • file-k20.xml

  • file-k100.xml

  • file-M10.xml

  • M20.xml

  • file-M100.xml

  • file-10.xml
  • file-20.xml
  • file-100.xml
  • file-k10.xml
  • file-k20.xml
  • file-k100.xml
  • file-M10.xml
  • file-M20.xml
  • file-M100.xml

男孩很高兴linux命令行的力量救了我。 (这不适合我的情况,因为 ls -1 我有一个命令是另一行或两个长)

Whew! Boy glad the "power of the linux command line" saved me there. (This isn't practical for my situation, because instead of ls -1 I have a command that is another line or two long)

现在,Windows的行为很简单,优雅,并且你希望它99%的时间做。为什么我不能在linux中?为什么哦为什么排序没有一个自动排序号码,不能让我撞到墙上切换?

Now, the Windows behavior is simple, elegant, and does what you want it to do 99% of the time. Why can't I have that in linux? Why oh why does sort not have a "automagic sort numbers in a way that doesn't make me bang head into wall" switch?

这是C ++的伪代码:

Here's the pseudo-code for C++:

bool compare_two_strings_to_avoid_head_injury(string a, string b)
{
    string::iterator ai = a.begin();
    string::iterator bi = b.begin();
    for(; ai != a.end() && bi != b.end(); ai++, bi++)
    {
        if (*ai is numerical)
            gobble up the number incrementing ai past numerical chars;
        if (*bi is numerical)
            gobble up the number incrementing bi past numerical chars;
        actually compare *ai and *bi and/or the gobbled up number(s) here
            to determine if we need to compare more chars or can return the 
            answer now;
    }
    return something here;
}

这么难吗?有人可以把这个排序,送我一份吗?

Was that so hard? Can someone put this in sort and send me a copy? Please?

推荐答案

这是我的第一个想法:

ls -1 | sed 's/\-\([kM]\)\?\([0-9]\{2\}\)\./-\10\2./' | sort | sed 's/0\([0-9]\{2\}\)/\1/'

基本上,我只是使用 sed 用零填充数字,然后再次使用它去除前导零。

Basically I just use sed to pad the number with zeros and then use it again afterwards to strip off the leading zero.

我不知道在Perl中是否更快。

I don't know if it might be quicker in Perl.

这篇关于如何从linux命令行数字排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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