python regex命令行工具,用于替换类似于“perl-ne”的功能。
red的Python项目详细描述
python regex命令行工具,用于替换类似于“perl-ne”的功能。
安装
从项目根目录:
$ python setup.py install
用法
使用–help/-h查看有关参数的信息:
$ red --help
可用的特殊变量:
line: | for the entire line that was matched, without the trailing newline |
---|---|
g: | for groups, which you index to get that match group, as g[0] in “(w+) .*” |
ag: | for aggregated groups with the -a option, which is a list of all match groups |
d: | for the groupdict, which you can index by named group as d[“foo”] in “(?P<foo>w+) .*” |
ad: | for the aggregated group dicts, a list of dicts |
示例用法:
$ cat test.txt foo 1 bar 2 fiz 5 baz 10 funk 10 bunk 9 funk a bunk b a b c d aaaaa bbbb cc
像grep一样使用:
$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" foo 1 bar 2 fiz 5 baz 10 funk 10 bunk 9
它也可以使用path作为参数,,但是路径必须直接出现在regex之后(argparse的奇怪之处)。:
$ red "\w+ (\d+) \w+ (\d+)" test.txt
使用它计算存储在变量g中的组上的python代码:
$ red "\w+ (\d+) \w+ (\d+)" test.txt -e "int(g[0]) + int(g[1])" 3 15 19
导入任意库并执行任何操作:
$ red '\w+ (\d+) \w+ (\d+)' test.txt -i json -e '"{} => {}".format(line, json.dumps(g))' foo 1 bar 2 => ["1", "2"] fiz 5 baz 10 => ["5", "10"] funk 10 bunk 9 => ["10", "9"]
如果要在eval之前执行一些python语句,也可以使用-x来完成此操作:
$ red "(\w+) (\d+).*" test.txt -x 'x = int(g[1]) ; y = g[0][::-1]' -e '(x, y)' (1, 'oof') (5, 'zif') (10, 'knuf')
使用它在所有stdin中聚合成列表ag:
$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" -a "sum([int(x[0]) for x in ag])" 16
在聚合之前,对于exec,聚合有自己的-x:
$ red "(\w+) (\d+).*" test.txt -X 's = sum(int(g[1]) for g in ag)' -a 's' 16
对每一个匹配项进行求值,并根据所有匹配项进行聚合:
$ cat test.txt | red "\w+ (\d+) \w+ (\d+)" -a "sum([int(x[0]) for x in ag])" -e "'adding {}'.format(g[0])" adding 1 adding 5 adding 10 16
也可以使用命名组,存储在变量d中并聚合到ad:
$ cat test.txt | red "\w+ (?P<first>\d+) \w+ \d+" -e "'first value is {first}'.format(**d)" first value is 1 first value is 5 first value is 10
即使是多行也可以工作,因为传入-x的内容只是exec'd:
$ red '.*' test.txt -x ' # if you want multiline, just hit apostrophe and press enter # and start typing if line.startswith("foo"): print("Line started with foo: {}".format(line)) ' Line started with foo: foo 1 bar 2
发挥创造力!:
$ cat urls.txt https://www.google.com/ http://www.yahoo.com/ http://www.example.com/foo $ red "(.*)" urls.txt -i requests -x 'response = requests.get(line)' -e '[response.status_code, response.content[:20]]' [404, '<!doctype html>\n<htm'] [200, '<!doctype html><html'] [200, '<?xml version="1.0" '] $ cat somelogfile.log INFO: visited http://www.example.com/foo ERROR: visited https://www.google.com/ ERROR: visited http://www.yahoo.com/ $ cat somelogfile.log | red '(?P<debuglevel>ERROR|INFO): \S+ (?P<url>.*)' -i requests,collections -x 'response = requests.get(d["url"])' -e '(response.status_code, response.content[0:20])' -a 'collections.Counter([d["debuglevel"] for d in ad]).items()' (404, '<!doctype html>\n<htm') (200, '<!doctype html><html') (200, '<?xml version="1.0" ') [('INFO', 1), ('ERROR', 2)]
免责声明
-e和-a中的代码是通过eval运行的,因此如果您粘贴某人的代码, 它会跑的。只是说。只运行您信任的代码,或亲自检查过的代码。 使用-i导入的任何库也一样。
发行说明
0.2.1: | Added -x for pre-eval exec, added -i for importing libraries, and -X for aggregate exec |
---|---|
0.2.0: | Added -i/–import functionality |
0.1.1: | Path works as CLI arg |
0.1.0: | Version is available on pypi, with functionality of evaluation and aggregation |
0.0.1: | Project created |