【问题标题】: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] 我想要的输出是排序后的内容。
【问题讨论】:
标签:
bash
shell
sorting
find
piping
【解决方案1】:
为了完整起见,这里还有其他几种方法:
find -maxdepth 1 -type f -iname '*key*' -not -name '*~' -exec cat {} \; | sort
find -maxdepth 1 -type f -iname '*key*' -not -name '*~' | xargs cat | sort
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,排序后的输出就会打印到终端窗口。