如何以递归方式执行具有多个管道的猫

时间:2021-06-21 16:04:33

I have some directories and files that looks like this:

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

/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

/ 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

These are CSV files, and I'm trying to get the 3rd column counted, and sorted from highest to lowest.

这些是CSV文件,我试图计算第3列,并从最高到最低排序。

Right now I'm able to accomplish this, but only within each directoryx specifically. For example, if I run this:

现在我能够完成这个,但仅限于每个目录x中。例如,如果我运行这个:

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

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

Then I get exactly what I want, but only with the data in that directory. I want to cat everything from all of the /my/directories/ sub directories into a single file.

然后我得到我想要的,但只有该目录中的数据。我想把所有/ my / directories / sub目录中的所有东西都放到一个文件中。

I've tried to use ls or find to accomplish this, but I can't get it working. I know you can recursively cat this way, similar to this:

我试图使用ls或者找到完成这个,但我无法让它工作。我知道你可以递归地用这种方式捕捉,类似于:

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

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

But, I haven't been able to get this to work with a multi piped command. Any help is appreciated.

但是,我无法使用多管道命令来实现此功能。任何帮助表示赞赏。

2 个解决方案

#1


1  

Try with xargs:

尝试使用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
----

#2


1  

find . -name "File*" | xargs cut -d, -f3 | sort | uniq -c | sort -nr

#1


1  

Try with xargs:

尝试使用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
----

#2


1  

find . -name "File*" | xargs cut -d, -f3 | sort | uniq -c | sort -nr