簡體   English   中英

Bash:按內容從'find'對文件進行排序

[英]Bash: Sort files from 'find' by contents

我有這條線:

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

我想提取所有文件的內容(這應該是文本),並返回到該管道sort以字母順序排序。 我已經嘗試將上面一行的輸出直接用於sort但這導致文件名被排序而不是它們的內容。 我是否需要將find的輸出轉換為數組然后按sort處理?

[edit]我想要的輸出是排序的內容。

為了完整起見,還有以下幾種方法:

  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

如果要將已排序的輸出保存到文件中,請嘗試:

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

否則只需刪除> sorted.txt ,排序的輸出將打印到終端窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM