bash – 如何拆分文件并且并行处理它们,然后将它们缝合回来? UNIX

我有一个文本文件infile.txt:
abc what's the foo bar.
foobar hello world,hhaha cluster spatio something something.
xyz trying to do this in parallel
kmeans you're mean,who's mean?

文件中的每一行都将被这个perl命令处理成out.txt

`cat infile.txt | perl dosomething > out.txt`

想象一下,如果文本文件是100,000,000行.我想并行化bash命令,所以我尝试这样的东西:

$mkdir splitfiles
$mkdir splitfiles_processed
$cd splitfiles
$split -n3 ../infile.txt
$for i in $(ls); do "cat $i | perl dosomething > ../splitfiles_processed/$i &"; done
$wait
$cd ../splitfiles_processed
$cat * > ../infile_processed.txt

但是还有一个比较冗长的做法吗?

来自@Ulfalizer的答案给您一个很好的解决方案提示,但它缺乏细节.

您可以使用GNU parallel(apt-get install Debian)

所以你的问题可以使用以下命令解决:

parallel -a infile.txt -l 1000 -j10 -k --spreadstdin perl dosomething > result.txt

这是论证的意思

-a: read input from file instead of stdin
-l 1000: send 1000 lines blocks to command
-j 10: launch 10 jobs in parallel
-k: keep sequence of output
--spreadstdin: sends the above 1000 line block to the stdin of the command

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...