我想使用 xargs
来计算压缩文件列表中 4 行的 block 数,并使用 8 个 CPU 并行计算,如下所示:
find $PWD/ -name "*.ext.gz" | xargs -t -n1 -P8 -I % gunzip -c % | paste - - - - | wc -l
目前,这个单行程序进行计算,但除了最后一个,我看不到输出计数。
我需要添加什么才能看到来自与输入文件关联的 wc -l
的数字?
有什么想法吗?
最佳答案
如果我对你的问题的理解是正确的,那么你的假设是错误的。看起来你期望的是
gunzip -c <filename> | paste - - - - | wc -l
将为 find
报告的每个文件运行。这是不正确的。实际发生的是
gunzip -c <filename>
正在为每个文件运行,每个未压缩文件的输出都被合并成一个大体,并且粘贴 - - - - | wc -l
正在该组合结果上运行。
更好的方法是编写一个简短的 shell 脚本,比如 count_groups.sh
,它看起来像这样:
#!/bin/bash
nlines=$(gzcat $1 | wc -l)
(( ngroups = nlines / 4 ))
echo "$1 : $ngroups"
然后,运行chmod +x count_groups.sh
,然后运行
find $PWD/ -name "*.ext.gz" | xargs -t -n1 -P8 -I% ./count_groups.sh %
关于linux - xargs 打印 wc -l 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335168/