Executing grep from Ruby

I want to run the following grep from within a Ruby script:

grep "word1" file1.json | grep -c "word2"

This will find the number of lines in the file with both words appearing. I could do this with Ruby regex, but it seems that Unix grep is much faster. So my question is how do I run this command within a script and return the result back to a Ruby variable?

I'd love to hear alternative solutions that don't use grep, if they are as fast or even faster.


使用反引号执行shell命令:

result = `grep "word1" file1.json | grep -c "word2"`
链接地址: http://www.djcxy.com/p/25306.html

上一篇: Rails:可以检查一个字符串是否是二进制的?

下一篇: 从Ruby执行grep