关于代码性能的问题:我试图对一个 ~20g 的文本文件运行 ~25 条正则表达式规则。该脚本应将匹配项输出到文本文件;每个正则表达式规则都会生成自己的文件。参见下面的伪代码:
regex_rules=~/Documents/rulesfiles/regexrulefile.txt
for tmp in *.unique20gbfile.suffix; do
while read line
# Each $line in the looped-through file contains a regex rule, e.g.,
# egrep -i '(^| )justin ?bieber|(^| )selena ?gomez'
# $rname is a unique rule name generated by a separate bash function
# exported to the current shell.
do
cmd="$line $tmp > ~/outputdir/$tmp.$rname.filter.piped &"
eval $cmd
done < $regex_rules
done
一些想法:
有没有办法只循环一次文本文件,评估所有规则并一次拆分为单个文件?这样会更快吗?
我应该为这项工作使用不同的工具吗?
谢谢。
最佳答案
这就是 grep
有一个 -f
选项的原因。将您的 regexrulefile.txt
减少到只有正则表达式,每行一个,然后运行
egrep -f regexrulefile.txt the_big_file
这会在单个输出流中生成所有匹配项,但您可以在之后对其执行循环操作以将它们分开。假设匹配的组合列表不是很大,这将是性能上的胜利。
关于regex - 在 bash 中 Grepping 一个 20g 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11676350/