[英]Bash: Sort files from 'find' by contents
我有這條線:
find -maxdepth 1 -type f -iname '*key*' -not -name '*~'
我想提取所有文件的內容(這應該是文本),並返回到該管道sort
以字母順序排序。 我已經嘗試將上面一行的輸出直接用於sort
但這導致文件名被排序而不是它們的內容。 我是否需要將find
的輸出轉換為數組然后按sort
處理?
[edit]我想要的輸出是排序的內容。
為了完整起見,還有以下幾種方法:
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
如果要將已排序的輸出保存到文件中,請嘗試:
find -maxdepth 1 -type f -iname '*key*' -not -name '*~' | cat | sort > sorted.txt
否則只需刪除> sorted.txt
,排序的輸出將打印到終端窗口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.