【问题标题】:How do I recursively perform a cat with multiple pipes如何递归执行具有多个管道的猫
【发布时间】:2016-09-10 03:34:32
【问题描述】:

我有一些看起来像这样的目录和文件:

/my/directories/directory0/ | -->File1.txt | -->File2.txt /my/directories/directory1/ | -->File1.txt | -->File2.txt /my/directories/directory2/ | -->File1.txt | -->File2.txt /my/directories/directory3/ | -->File1.txt | -->File2.txt

这些是 CSV 文件,我正在尝试计算第三列,并从高到低排序。

现在我能够做到这一点,但仅限于每个目录 x 内。例如,如果我运行这个:

cd /my/directories/directory0/ cat *.txt | awk -F "," '{print $3}' | sort | uniq -c | sort -nr > finalOutput.txt

然后我得到了我想要的,但只有那个目录中的数据。我想将所有 /my/directories/ 子目录中的所有内容都集中到一个文件中。

我尝试使用lsfind 来完成此操作,但无法正常工作。我知道你可以这样递归 cat,类似于:

find /my/directories/ -name '*.txt' -exec cat {} \; > finalOutput.txt

但是,我无法让它与多管道命令一起使用。任何帮助表示赞赏。

【问题讨论】:

  • find /my/directories/ -name '*.txt' -exec cat {} \; | awk ... 有什么问题?
  • 这并没有错。我只是不知道 -exec 标志之后的正确语法。这是一个有效的答案,因为它显示了我的语法错误。

标签: awk find cat


【解决方案1】:

试试 xargs:

$ find . -name "f?" | xargs awk -F, '{ print $3 }' | sort | uniq -c | sort -nr
4 3
1 z
1 three
1 c
1 5

$ find . -name "f?" -exec echo "File: " {} \; -exec cat {} \; -exec echo "----" \;
File:  ./d0/f1
1,2,3
----
File:  ./d0/f2
3,4,5
----
File:  ./d1/f1
8,9,3
----
File:  ./d1/f2
a,b,c
----
File:  ./d2/f1
x,y,z
----
File:  ./d2/f2
one,two,three
----
File:  ./d3/f1
red,yellow,3
----
File:  ./d3/f2
1,2,3
----

【讨论】:

  • 这行得通。我还想向任何找到这个答案的人指出,William Pursell 对我原来的问题的评论也解决了这个问题,但是通过修复我原来的尝试。
【解决方案2】:
find . -name "File*" | xargs cut -d, -f3 | sort | uniq -c | sort -nr

【讨论】:

  • 这只返回一列,不做计数。
  • @trueCamelType 好吧,你的例子也没有,你说“那我得到了我想要的”。所以,基本上,我不明白你想要完成什么。
  • 由于我的示例中的 uniq 命令,它确实返回了不止一列。它还计算出现次数。
  • @trueCamelType 我想我现在明白了。无论目录深度如何,使用find 查找所有相关文件,并使用cut 而不是awk 选择单个列。然后,像原来一样继续。
猜你喜欢
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
相关资源
最近更新 更多