【问题标题】:Bash: Sort files from 'find' by contentsBash:按内容对“查找”中的文件进行排序
【发布时间】:2013-08-10 03:25:10
【问题描述】:

我有电话:

find -maxdepth 1 -type f -iname '*key*'  -not -name '*~'

我想提取所有返回文件的内容(应该是文本)并将其通过管道传输到sort 以按字母顺序排序。我尝试将上述行的输出直接传送到sort,但这会导致对文件名进行排序,而不是对它们的内容进行排序。是否需要将find的输出转成数组,然后由sort处理?

[edit] 我想要的输出是排序后的内容。

【问题讨论】:

  • 通过xargs cat 管道,然后通过sort
  • 谢谢!效果很好

标签: bash shell sorting find piping


【解决方案1】:

为了完整起见,这里还有其他几种方法:

  1. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' -exec cat {} \; | sort
  2. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' | xargs cat | sort
  3. cat $(find -maxdepth 1 -type f -iname '*key*' -not -name '*~') | sort

【讨论】:

    【解决方案2】:

    如果您想将排序后的输出保存到文件中,请尝试:

    find -maxdepth 1 -type f -iname '*key*'  -not -name '*~' | cat | sort > sorted.txt
    

    否则只要去掉> sorted.txt,排序后的输出就会打印到终端窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      相关资源
      最近更新 更多