grep 处理大文件太慢的方法
1.更换编码方式
LC_ALL=C fgrep -m 1 "content" filename
命令解释:
- 默认文本都采用UTF-8所以比较慢,因此编码方式换成C语言环境(ascii)
- grep 带有正则,fgrep则为纯文本匹配,因此会比grep更快
- -m 1 只匹配一次,匹配到就终止匹配(根据自己需求调整)
2.并发方式
cat filename | parallel --pipe fgrep -m 1 "con"
命令解释:
- 使用parallel来并发fgrep进程(不加 -j 指定并发数的话,默认每个核分配一个)
参考:
-
https://stackoverflow.com/questions/13913014/grepping-a-huge-file-80gb-any-way-to-speed-it-up
-
https://www.inmotionhosting.com/support/website/speed-up-grep-searches-with-lc-all/