我如何并行地grep【JAVA教程】

!
也想出现在这里? 联系我们
信息

我如何并行地grep,第1张

概述我如何并行地grep

我通常使用grep -rIn pattern_str big_source_code_dir中find一些东西。 但grep不是平行的,我该如何平行? 我的系统有4个内核,如果grep可以使用所有内核,速度会更快。

grep文本与通配符inbetween

将列表转换为双引号逗号分隔的string

使用UNIX删除文本文件中的某些行

现代的Unix工具是用什么编程语言编写的?

如何使用BASH比较两个文本文件的相同的确切文字?

如果您使用HDD来存储您正在搜索的目录,则不会有速度改进。硬盘驱动器几乎是单线程访问单元。

但是,如果你真的想做并行grep,那么这个网站给出了两个提示,如何使用find和xargs来做到这一点。 例如

find . -type f -print0 | xargs -0 -P 4 -n 40 grep -i foobar

GNU parallel命令对此非常有用。

sudo apt-get install parallel # if not available on debian based systems

然后, paralell手册页提供了一个例子:

EXAMPLE: Parallel grep grep -r greps recursively through directorIEs. On multicore cpus GNU parallel can often speed this up. find . -type f | parallel -k -j150% -n 1000 -m grep -H -n STRING {} This will run 1.5 job per core,and give 1000 arguments to grep.

你的情况可能是:

find big_source_code_dir -type f | parallel -k -j150% -n 1000 -m grep -H -n pattern_str {}

最后,GNU并行手册页还提供了一个描述xargs和parallel命令之间差异的部分,这有助于理解为什么在你的情况下并行更好

DIFFERENCES BETWEEN xargs AND GNU Parallel xargs offer some of the same possibilitIEs as GNU parallel. xargs deals badly with special characters (such as space,\’ and \”). To see the problem try this: touch important_file touch \’not important_file\’ ls not* | xargs rm mkdir -p \”My brother\’s 12\” records\” ls | xargs rmdir You can specify -0 or -d \”n\”,but many input generators are not optimized for using Nul as separator but are optimized for newline as separator. Eg head,tail,awk,ls,echo,sed,tar -v,perl (-0 and instead of n),locate (requires using -0),find (requires using -print0),grep (requires user to use -z or -Z),sort (requires using -z). So GNU parallel\’s newline separation can be emulated with: cat | xargs -d \”n\” -n1 command xargs can run a given number of jobs in parallel,but has no support for running number-of-cpu-cores jobs in parallel. xargs has no support for grouPing the output,therefore output may run together,eg the first half of a line is from one process and the last half of the line is from another process. The example Parallel grep cannot be done reliably with xargs because of this. …

请注意,您需要在并行grep搜索词中转义特殊字符,例如:

parallel –pipe –block 10M –ungroup LC_ALL=C grep -F \’PostTypeID=\”1\”\’ < ~/Downloads/posts.xml > questions.xml

使用独立的grep, grep -F \’PostTypeID=\”1\”\’可以在不转义双引号的情况下工作。 我花了一段时间才弄明白这一点!

还要注意使用LC_ALL=C和-F标志(如果您只是搜索完整的字符串)以获得更多的加速。

总结

以上是内存溢出为你收集整理的我如何并行地grep全部内容,希望文章能够帮你解决我如何并行地grep所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞71 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容