猫从一系列文件中读取

时间:2013-07-18 21:58:31

标签: bash shell unix

我每天都会在目录结构中收集一些tsv文件,这些文件看起来像/ tmp / data / $ yearmonth / $ day / $ hour。因此/ tmp / data / $ yearmonth / $ day

中有24个目录

我有这样的shell脚本:

yearmonth=`date -d "-2 days" +%Y%m`
day=`date -d "-2 days" +%d`

files=()
cd /tmp/data/$yearmonth/$day
for i in `ls -a */*.tsv`
do
  files+=($i)
done

数组文件中存储了所有tsv文件。我想把所有这些tsvfiles“cat”到一个单独的tsvfiles,并希望对它执行sort | uniq -c。 我怎么做? 随着tsv文件变得庞大,猫可以变得非常慢。可能是另一种选择。 感谢

1 个答案:

答案 0 :(得分:1)

您要显示的代码存在一些问题:

  1. 如果你有足够的文件,或者你的子目录ls -a中的名字足够长,那么参数列表中的文件太多就会失败。标准的补救措施是使用find

    查找/ tmp / data / year / mon / day -type f -iname'* .tsv'-print0

  2. 一旦找到你可以管道文件列表,它就会直接生成排序

    | xargs -0 sort --unique

  3. 没有涉及cat,但当然,仍然需要找到并阅读这些文件。