簡體   English   中英

在一個命令中使用find sort和wc -l

[英]using find sort and wc -l in the one command

這是使用查找來查找文件並顯示每個文件中的行數的方式

$ find ./ -type f -name "data*.csv" -exec wc -l {} +
   380723 ./data_2016-07-07-10-41-13.csv
   369869 ./data_2016-07-11-10-42-01.csv
   363941 ./data_2016-07-08-10-41-50.csv
   378981 ./data_2016-07-12-10-41-28.csv
  1493514 total

如何按文件名對結果排序? 下面是我的嘗試,但是沒有用。

$ find ./ -type f -name "data*.csv" -exec wc -l {} + | sort
1493514 total
363941 ./data_2016-07-08-10-41-50.csv
369869 ./data_2016-07-11-10-42-01.csv
378981 ./data_2016-07-12-10-41-28.csv
380723 ./data_2016-07-07-10-41-13.csv
$ find ./ -type f -name "data*.csv" | sort -exec wc -l {} +
sort: invalid option -- 'e'
Try `sort --help' for more information.
$ find ./ -type f -name "data*.csv" -exec sort | wc -l {} +
find: wc: {}missing argument to `-exec'
: No such file or directory
wc: +: No such file or directory
0 total
$

有人可以提供解決方案並糾正我,以便我更好地理解嗎?

編輯1

man sort

  -k, --key=POS1[,POS2]
          start a key at POS1 (origin 1), end it at POS2 (default end of line).  See POS syntax below


   POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1.  If neither -t nor -b is in effect, characters in a field are counted from the begin‐
   ning of the preceding whitespace.  OPTS is one or more single-letter ordering options, which override global ordering options for that key.  If no key is given, use the entire line as the key.

Ismail關於使用sort -k的建議是正確的。 但是,我經常懶得學習(或重新學習) -k工作方式,因此這里有一個便宜的解決方案:

find . -name 'data*.csv' -print0 | sort -z | xargs -0 wc -l

編輯:經過一些試驗,我確實弄清楚了-k工作原理:

find . -name 'data*.csv' -exec wc -l {} + | sort -k 2

暫無
暫無

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

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