Best way to transpose sed regex from Shell to Python script

Viewed 52

I have a file containing these lines

Entry :  12300000
F Blocks:   0x00000020 0x00000000 0x000a1b00
S Blocks:   0x00100000 0x0000001c 0x00000150

Using a shell script, the hex values from line starting with F Blocks: can be extracted using the line below :

blocks="$(sed -nE 's/F Blocks:[\t ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)/0x\2 0x\4 0x\6/p' filename)"

I would like to do the same extract in a Python script, using the subprocess module

import subprocess
sed_cmd = ['sed', '-n', '-E', "s/F Blocks:[\t ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)/0x\\2 0x\\4 0x\\6/p", 'filename']
proc = subprocess.Popen(sed_cmd, stdout=subprocess.PIPE)
blocks = proc.stdout.read()

Is there a best practice to extract data and the output in a variable, or could it be simplify ?

1 Answers

Use plain Python:

results = []                                # Define a list for matches
with open(filepath,'r') as fr:              # Open the file stream
    for line in fr:                         # Read line by line
        if line.startswith("F Blocks:"):    # If line starts with our value
            results = line[line.find(':')+1:].split() # Get all after : and split with whitespace
            # break                         # Uncomment this if you needn't process the file any longer

print(results)

See an online demo.

Related