简体   繁体   中英

Remove all lines between the pattern

I would like to extract from the text:

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

this portion:

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

put it in a file and delete it from the initial file, which will look this way afterwards:

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

What I tried was:

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

but unfortunately it will stop after the first occurrence of . But the purpose is to delete all lines after RBE3 is met, which starts with * and this one will delete only one line. Thank you

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)
RBE3\*[^\n]*\n(?:\*[^\n]*\n)*

Try this.Replace with empty string .See demo.

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

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

Through python's re module.

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))

Update:

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))

here's a regexp that will work on Python or PCRE

/(RBE3\\*).+(?=CHEXA\\*)/s (note that s modifier is required for it to work.)

A simple python implementation :

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)

With 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

The nice thing about this is that it is easily extended for the inversion. If you write

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

then by replacing ignore=0 with ignore=1 , you can extract the block instead of ignoring it.

using awk:

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

output:

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

The above uses index() so it's doing a string rather than regexp comparison so it won't fail if your key contains RE metacharacters, unlike any sed solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM