删除图案之间的所有线条

时间:2014-12-18 13:16:47

标签: python awk sed

我想从文中摘录:

CHEXA*          99001088        99001001        99001143        99001179*00072A1
*00072A1        99001047        99001104        99001144        99001180*00072A2
*00072A2        99001048        99001105                                
RBE3*           99001089                        99001001             123*00072A5
*00072A50.11263443595303             123         6001515.041507658257159*00072A6
*00072A6         60016620.61808377914687             123         6001542
CHEXA*          99001086        99001001        99001128        99001095*0007299
*0007299        99001081        99001171                                *000729B
*000729B

这一部分:

RBE3*           99001089                        99001001             123*00072A5
*00072A50.11263443595303             123         6001515.041507658257159*00072A6
*00072A6         60016620.61808377914687             123         6001542

将它放在一个文件中,然后从初始文件中删除它,之后会看起来像这样:

CHEXA*          99001088        99001001        99001143        99001179*00072A1
*00072A1        99001047        99001104        99001144        99001180*00072A2
*00072A2        99001048        99001105                                
CHEXA*          99001086        99001001        99001128        99001095*0007299
*0007299        99001081        99001171                                *000729B
*000729B

我尝试的是:

sed '/RBE3\*/,/\*/d'

但不幸的是,它会在第一次出现后停止。但目的是在满足RBE3 之后删除所有行,以*开头,这个只删除一行。谢谢

7 个答案:

答案 0 :(得分:1)

import os

keep = True
with open(pathToInput) as infile, open(pathToOutput, 'w') as outfile, open(pathToSave) as savefile:
    for line in infile:
        if line.startswith("RBE3"):
            keep = False
        elif not line.startswith("*"):
            keep = True
        if keep:
            outfile.write(line)
        else:
            savefile.write(line)

os.remove(pathToInput)
os.rename(pathToOutput, pathToInput)

答案 1 :(得分:1)

RBE3\*[^\n]*\n(?:\*[^\n]*\n)*

尝试使用empty string替换。请参阅演示。

https://regex101.com/r/vN3sH3/3

print re.sub(r"RBE3\*[^\n]*\n(?:\*[^\n]*\n)*","",text)

答案 2 :(得分:1)

通过python' re模块。

import re
with open('/path/to/the/infile') as infile, open('/path/to/the/outfile', 'w+') as out:
    foo = infile.read()
    out.write(re.sub(r'(?s)RBE3\*.*?\n(?!\*)', r'', foo))

<强>更新

import re
with open('/path/to/the/infile') as infile, open('/path/to/the/outfile', 'w+') as out, open('/path/to/the/file/to/save/deleted/lines', 'w+') as save:
    foo = infile.read()
    out.write(re.sub(r'(?s)(.*?\n)(RBE3\*.*?\n(?!\*))(.*)', r'\1\3', foo))
    save.write(re.sub(r'(?s)(.*?\n)(RBE3\*.*?\n(?!\*))(.*)', r'\2', foo))

答案 3 :(得分:0)

这是一个可以在Python或PCRE上运行的正则表达式

/(RBE3\*).+(?=CHEXA\*)/s(请注意,s修饰符是必需的才能生效。)

一个简单的python实现:

import re
import os
inPut = "list"
outPut = "tmp"

regexp = re.compile("(RBE3\*).+(?=CHEXA\*)", re.S)

with open(inPut, 'r') as f:
    fileStr = f.read()
match = regexp.search(fileStr).group(0)
ret = re.sub(regexp, "", fileStr)
with open(outPut, 'w') as tmpFile:
    tmpFile.write(match)
os.remove(inPut)
os.rename(outPut, inPut)

答案 4 :(得分:0)

使用awk:

awk -v flag=0 '
    /^[^\*]/  { flag = 0 } # clear flag if the line does not start with a *
    /^RBE3\*/ { flag = 1 } # except if it is the starting line of an ignored block
    flag == 0 { print }    # print if ignore flag is not set.
  ' foo.txt

关于这一点的好处是它可以很容易地扩展到反转。如果你写

awk -v flag=0 -v ignore=0 '
    /^[^\*]/ { flag = 0 }
    /^RBE3\*/ { flag = 1 }
    flag != ignore { print }
  ' foo.txt

然后将ignore=0替换为ignore=1,您可以提取块而不是忽略它。

答案 5 :(得分:0)

使用awk:

awk '{if(match($0,"RBE3")>0)flag=0}{if(match($0,"CHEXA")>0)flag=1}{if(flag==1) print $0}' File

输出:

CHEXA*          99001088        99001001        99001143        99001179*00072A1
*00072A1        99001047        99001104        99001144        99001180*00072A2
*00072A2        99001048        99001105                                
CHEXA*          99001086        99001001        99001128        99001095*0007299
*0007299        99001081        99001171                                *000729B
*000729B

答案 6 :(得分:0)

awk -v key="RBE3" '
index($0,key"*")==1 { f=1; print > "newfile" }
f && /^\*/ { print > "newfile"; next }
{ f=0; print }
' file > tmp && mv tmp file

以上使用index(),因此它执行字符串而不是正则表达式比较,因此如果您的密钥包含RE元字符,它将不会失败,与任何sed解决方案不同。