tse是python中的一个输入流编辑器。
tse的Python项目详细描述
tse使用python表达式处理文本输入流。与awk一样,tse命令行选项也是一系列条件和操作:
tse -s COND1 ACTION1 -s COND2 ACTION2 ACTION3
例如,要查找以abc
开头的行$ tse -s '^abc' 'P(L)' -- *.*
查找包含url的行
$ tse -s 'http://\\S+' 'P(S0)' -s 'mailto://\\S+' 'print S0' \ -- *.*
转换大写
$ cat FILENAME | tse -p '.*' -a 'P(L.upper())'
此外,tse还可以用于执行python一行程序。
获取当前目录
$ tse -x 'P(os.getcwd())'
庆祝星期五
$ tse -ms datetime -x 'if 4==date.today().weekday():' \ '{{P("Thank God It\'s Friday")}}'
命令行选项
usage: tse [-h] [--statement PATTERN [ACTION ...]] [--execute EXECUTE [EXECUTE ...]] [--begin BEGIN [BEGIN ...]] [--end END [END ...]] [--ignore-case] [--field-separator FIELD_SEPARATOR] [--inplace EXTENSION] [--input-encoding INPUT_ENCODING] [--output-encoding OUTPUT_ENCODING] [--script-file SCRIPT_FILE] [--module MODULE] [--module-star MODULE_STAR] [--version] [FILE [FILE ...]] Text Stream Editor in Python positional arguments: FILE With no FILE, or when FILE is -, read standard input. optional arguments: -h, --help show this help message and exit --statement PATTERN [ACTION ...], -s PATTERN [ACTION ...] a pair of pattern and action(s). --execute EXECUTE [EXECUTE ...], -x EXECUTE [EXECUTE ...] execute script without reading files. --begin BEGIN [BEGIN ...], -b BEGIN [BEGIN ...] action invoked before input files have been read. --end END [END ...], -e END [END ...] action invoked after input files have been exhausted. --ignore-case, -i ignore case distinctions. --field-separator FIELD_SEPARATOR, -F FIELD_SEPARATOR regular expression used to separate fields. --inplace EXTENSION edit files in-place. --input-encoding INPUT_ENCODING, -ie INPUT_ENCODING encoding of input stream. --output-encoding OUTPUT_ENCODING, -oe OUTPUT_ENCODING encoding of output stream. --script-file SCRIPT_FILE, -f SCRIPT_FILE specifies an alternative script file. the default script file is ~/.tserc. --module MODULE, -m MODULE modules to be imported. --module-star MODULE_STAR, -ms MODULE_STAR modules to be imported in form of "from modname import *". --version show program's version number and exit
模式和操作
--statement选项接受模式和操作。
模式是用于搜索行的正则表达式。当在行中找到相应的模式时执行操作。此命令打印文件名中包含“abc”的行。
$ cat FILENAME | tse -s "abc" "print(L)"
如果模式匹配,则不执行以下模式/操作对。此命令打印文件名中不以开头的行。
$ cat FILENAME | tse -s "'#" "pass" ".*" "P(L)"
空模式表示.\*,空操作表示print(L)。因此,tse -s '' ''相当于tse -s'.*' 'P(L)'
--statement选项中的操作参数与\n联接。因此,您可以编写
tse -s '.*' 'for c in L:' ' print(c)'
^操作中的{tt9}$和}}将转换为换行+缩进/删除。例如,
'if L1:{{for c in L2:{{print(c)}}else:{{print(L3)}}}}else:{{print(L4)}}'
转换为
if L1: for c in L2: print(c) else: print(L3) else: print(L4)
^忽略字符串文本和注释中的{tt9}$和}}。
–执行选项
使用--execute选项指定的python脚本在不读取输入文件的情况下被执行。这可以用作python的一行程序执行器。
# sample to post message to Discord chat $ tse -ms requests -x 'P(post("https://discordapp.com/api/webhooks/XXX/YYY",'\ 'json=dict(username="username", content="test")))'
–开始和–结束选项
在读取输入流之前,将执行使用--begin选项指定的python脚本。使用--end选项指定的python脚本在输入流用完后执行。
- ::
- 样本统计目录中文件的所有字母 $tse–开始'n=0'–结束'p(n)'-s'''n+=len(l)–。
变量
以下变量可以在action语句中使用。
FILENAME: | The name of file currently reading. |
---|---|
F: | The pathlib.Path object of the file currently reading. |
LINENO: | Line numberof the current line. |
L: | Current line. |
L0: | Current line. |
L1, L2: | Fields of the current line separeted by whitespace. |
N: | Number of fileds. |
S: | Part of text matched to condition regex. |
S0, S1, …: | sub-string matched to condition regex. S0 is entire matched part, S1, S2 are sub group of condition regex. |
(name): | If condition regex has group names defined by ^{tt16}$, sub-string could be referenced by variable ^{tt17}$. |
M: | Match object. |
E: | Function to call subprocess.check_output(). ^{tt18}$ is equevalent to ^{tt19}$. |
P: | (Python3 only) Function to call print(). ^{tt20}$ is equevalent to ^{tt21}$. |
C: | The pathlib.Path object of the current directory. |
预导入模块
以下模块导入如下:
import sys, os, re from os import path from glob import * from pathlib import * # Only if pathlib is installed.
脚本文件
如果文件{TT22}$存在,则在开始时执行文件。在脚本文件中,您可以导入faivite模块,或者编写您喜欢的方便的函数。scipt文件中定义的值可由命令选项中指定的操作访问。
命令替换
在python3中,反勾号中的字符串作为命令执行。字符串`ls~`等同于subprocess.check_output('ls ~', shell=True, universal_newline=True)。
在python 3.6或更高版本中,支持f前缀:
ls | tse -s '\.txt' 'P(f`cat {L}`)'
示例
在每行输入流中打印数字字符的总和:
tse -s "\d+" \ "print(sum(int(s) for s in re.findall(r"\d+", L)))" \ -- *.*
对所有行中的所有数字字符求和:
tse -b "all=0" \ -s "\d+" "all+=sum(int(s) for s in re.findall(r"\d+", L)))" \ -e "P(all)" -- *.*
查找当前目录中的所有扩展部分:
ls | tse --begin 'exts=set()' --end 'P(exts)' \ -s '' 'exts.add(Path(L).suffix)'