将sed正则表达式转换为python代码 [英] Converting a sed regular expression to python code

查看:36
本文介绍了将sed正则表达式转换为python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能理解下面的sed正则表达式.

I can understand the following sed regular expression.

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

我的任务是将此 bash 行转换为 Python 代码.什么是最好的方法来做到这一点?简单地调用 os.system(cmd) 上面的 cmd ?或者使用 Python re 模块?任何伪代码都是最受欢迎的.:)

I have the task of converting this bash line to Python code. What is best way to do this? Simply invoke os.system(cmd) with above as cmd ? Or use the Python re module ? Any pseudo code is most welcome. :)

推荐答案

你问的是最好的方法,我只是给你一个简单的方法.你肯定可以优化它.但是,仍然值得对您的约束进行测试,因为调用 shell 需要一些时间.
值得注意的是,shell 中的管道可能是获得更快代码的好方法,因为 sed 可以开始工作而无需等待 cat 完成.sort 也可以开始它的工作,但显然只有在 sed 完成工作时才会输出.因此,这是在 IO 期间使用 CPU 的好方法,应该被视为一种省力/性能好的解决方案.
我试过一个简单的例子,但你会明白的:

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 :

test 中:

love
lol
loki
loki
ki
loutre
poutre

简单的 bash 命令,看起来像你的:

Simple bash command, looking like yours :

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

输出:

ki
l
poutre
utre
ve

现在让我们尝试在 python 中做同样的事情:

Now let's try to do 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 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'))))))

这篇关于将sed正则表达式转换为python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆