Converting sed standard expression to python code

I can understand the following sed regex.

 sed 's/.*\(SNAP=[^|]*\) |.*/\1/' | sort | uniq -c > $log.snaps

      

I have a task to convert this bash string to Python code. What's the best way to do this? Just call os.system(cmd)

above like cmd? Or use a Python module re

? Any pseudocode is appreciated. :)

+3


source to share


1 answer


You asked for the best, I just give you a simple one. You could optimize it. Still, it's worth checking with your constraints, as the shell invocation takes some time.
It's worth noting that wrapped pipes can be a great way to have faster code, as they sed

can start running without waiting for completion cat

. sort

will also be able to start its work, but will obviously only output when sed is running. So this is a great way to use yours CPU

during your IOs and should be considered a low performance / good performance solution.
I've tried with a simple example, but you get the idea:

In test

:

love
lol
loki
loki
ki
loutre
poutre

      

A simple bash command similar to yours:

cat test | sed 's/lo\(.*\)$/\1/' | sort | uniq

      

Outputs:



ki
l
poutre
utre
ve

      

Now try doing the same in python:

#!/usr/bin/python

import re

s = """love
lol
loki
loki
ki
loutre
poutre"""

arr = s.split('\n')                                             # sed iterates on each line
arr = map((lambda line: re.sub(r'lo(.*)$', r'\1', line)), arr)  # sed
arr = set(arr)                                                  # uniq
arr = sorted(list(arr))                                         # sort

print '\n'.join(arr)                                            # output it

      

This can also be written in an ugly line of code:

print '\n'.join(sorted(list(set(map((lambda line: re.sub(r'lo(.*)$', r'\1', line)), s.split('\n'))))))

      

+2


source







All Articles