linux - grep 和 grep -v 同时

标签 linux grep

我必须为 3 个不同的字符串搜索一个非常大的文本文件 (~15GB)。需要注意的重要一点是 X、Y 和 Z 这三个字符串占文本文件的大约 99.9%,并且互斥。

即 X + Y + Z + .01% 垃圾 = 完整文件

那么有什么方法可以通过同时执行 grep 和 grep -v 函数来减少时间 因此逻辑流程应该是:

grep X filename.txt >> linescontainingstringX.
somehow do grep Y and grep Z on the remaining file ie grep Y >> linesnotcontainingstringX

如果有适合我的方法,请告诉我。

最佳答案

使用 perl。

perl -n -e 'BEGIN{ open XFILE,">x.txt" or die "$!" ; open YFILE,">y.txt"  or die "$!"; open ZFILE, ">z.txt" or die "$!";} print XFILE $_ if /X/; print YFILE $_ if /Y/; print ZFILE $_ if /Z/;'

关于linux - grep 和 grep -v 同时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907138/

相关文章:

c - 从 Linux 内核模块的文件描述符获取文件名/路径?

include - grep --include 选项的用法?

linux - SMTP 警报不适用于 keepalived

linux - 如何在linux中运行.cc扩展名的文件?

bash - 如何对 grep 返回的文件内容进行 grep?

linux - 如何在 linux bash 中获取具有最大 CPU 使用率的任务名称?

linux - 用于解析命令输出的 Bash 脚本

linux - 将 f06 文件的一部分复制到一个新的文本文件中

python - 如何让 Python 2.7 处理包含 "&& exit"的 shell 脚本?

linux - 如何使用 perl 脚本和 ssh 在远程 linux 服务器中运行 bash 或 shell 脚本