Skip to content Skip to sidebar Skip to footer

Converting A Sed Regular Expression To Python Code

I can understand the following sed regular expression. sed 's/.*\(SNAP=[^|]*\) |.*/\1/' | sort | uniq -c > $log.snaps I have the task of converting this bash line to Pytho

Solution 1:

You asked for the best way, I'm just giving you a simple one. You could surely optimize it. But still, it is worth testing with your constraints, since invoking a shell takes some time. It should be worth noting that pipes in shell might be a great way to have faster code, since sed can start to work whithout waiting for cat to finish. sort will also be able to begin its work but obviously will only output when sed is done working. So it is a great way to use your CPU during your IOs and should be considered as a low effort/good performance solution. I've tried with a simple example, but you will get the idea :

In test :

love
lol
loki
loki
ki
loutre
poutre

Simple bash command, looking like yours :

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

Outputs :

ki
l
poutre
utre
ve

Now let's try to do the same in python :

#!/usr/bin/pythonimport 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))                                         # sortprint'\n'.join(arr)                                            # output it

This could also be written in a ugly line of code :

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

Post a Comment for "Converting A Sed Regular Expression To Python Code"