Pythonpy命令行版“瑞士军刀”
Pythonpy 可看作是命令行版的“瑞士军刀”,能在命令行中直接执行任何 Python 指令。
Usage
浮点运算
$ py '3 * 1.5'
4.5
自动导入任意模块
$ py 'math.exp(1)'
2.71828182846
$ py 'random.random()'
0.103173957713
$ py 'datetime.datetime.now?'
Help on built-in function now:
now(...)
[tz] -> new datetime with tz's local day and time.
py -x'foo(x)'将 foo 应用到输入的每行
将输入的各行乘以7
$ py 'range(3)' | py -x 'int(x)*7'
0
7
14
抓住 csv 的第二列
$ echo $'a1,b1,c1\na2,b2,c2' | py -x 'x.split(",")[1]'
b1
b2 将 “.text” 附加到目录中的每个文件
$ ls | py -x '"mv `%s` `%s.txt`" % (x,x)' | sh
# sharp quotes are swapped out for single quotes
# single quotes handle spaces in filenames
删除 find 命令返回的每个文件
$ find . -type f | py -x '"rm %s" % x' | sh
只获取2位数字
$ py 'range(14)' | py -x 'x if len(x) == 2 else None'
10
11
12
13
评论
