【问题标题】:using find sort and wc -l in the one command在一个命令中使用 find sort 和 wc -l
【发布时间】:2016-07-12 00:04:39
【问题描述】:

这是使用 find 查找文件并显示每个文件中的行数的方法

$ 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.

【问题讨论】:

  • 你可以查看-k标志到sort
  • @IsmailBadawi 你能澄清一下完整的命令是什么吗?好的,有人在下面的答案中发布。

标签: bash sorting find wc


【解决方案1】:

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

【讨论】:

  • tks.2sort -k 2 中做了什么?
猜你喜欢
  • 2020-05-10
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
相关资源
最近更新 更多