Grep 从大文件(TCL或shell)中删除多个长行

Grep 从大文件(TCL或shell)中删除多个长行,grep,tcl,Grep,Tcl,我有一个2.5G长的ascii文件,大约有370万行。有些线很长。行将包含有趣的字符,cmds可能会将其解释为转义字符或特殊字符。(斜杠、反斜杠、各种大括号等) 我有一系列特定的grep cmds,它们将从文件中提取16行。我想从大文件中删除这16行 grep pat1 bigfile | grep -v pat2 | grep -v pat3 | grep -v pat4 > temp temp中的线路约10MB长 现在我想反转该选择,以便从bigfile中删除temp中的文件 我试过

我有一个2.5G长的ascii文件,大约有370万行。有些线很长。行将包含有趣的字符,cmds可能会将其解释为转义字符或特殊字符。(斜杠、反斜杠、各种大括号等)

我有一系列特定的grep cmds,它们将从文件中提取16行。我想从大文件中删除这16行

grep pat1 bigfile | grep -v pat2 | grep -v pat3 | grep -v pat4 > temp
temp中的线路约10MB长

现在我想反转该选择,以便从bigfile中删除temp中的文件

我试过了

grep -v -f temp bigfile > newbigfile
结果是“grep:内存耗尽”

我可以使用unixshell和简单的TCL脚本来完成这项工作

谢谢
Gert

虽然在内存中保留几十MB对于Tcl程序来说是微不足道的,但如果可以的话,您不希望同时在内存中保留所有2.5GB。这意味着我们希望将要排除的行保留在内存中,并通过以下方式传输数据:

# Load the exclusions into a list
set f [open "temp"]
set linesToExclude [split [read $f] "\n"]
close $f

# Stream the main data through...
set fIn [open "bigfile"]
set fOut [open "newbigfile" "w"]
while {[gets $fIn line] >= 0} {
    # Only print the line if it isn't in our exclusions
    if {$line ni $linesToExclude} {  # 'ni' for Not In
        puts $fOut $line
    }
}
close $fOut
close $fIn
一般来说,我不想处理长度超过几百字节的文本行。除此之外,它开始感觉像是在处理二进制数据,即使它是正式的文本…

名称“temp”表明您并不真正需要该文件。然后,您可以在Tcl中这样做:

set fIn [open "bigfile"]
set fOut [open "newbigfile" "w"]
while {[gets $fIn line] >= 0} {
    # Skip the unwanted lines
    if {[regexp pat1 $line] && \
      ![regexp pat2 $line] && \
      ![regexp pat3 $line] && \
      ![regexp pat4 $line]} continue
    # Print lines that made it through
    puts $fOut $line
}
close $fOut
close $fIn

我不知道执行转换所需的时间是什么,或者这是否是一个问题。

使用旧版本的Tcl而不使用
ni
?升级或者使用
[lsearch-exact$line stoexclude$line]==-1