NAME

App::Greple::tee - 用外部命令结果替换匹配文本的模块

SYNOPSIS

greple -Mtee command -- ...

VERSION

Version 1.05

DESCRIPTION

Greple 的 -Mtee 模块将匹配到的文本片段发送给指定的过滤命令,并用该命令的结果替换。这个想法源自名为 teip 的命令。它就像把部分数据旁路到外部过滤命令。

过滤命令紧跟在模块声明(-Mtee)之后,并以两个短横线(--)结束。例如,下面的命令对数据中匹配到的单词调用 tr 命令,并传入 a-z A-Z 参数。

greple -Mtee tr a-z A-Z -- '\w+' ...

上面的命令把所有匹配到的单词从小写转换为大写。事实上,这个例子本身不太实用,因为 greple 使用 --cm 选项可以更高效地完成同样的事情。

默认情况下,命令作为单个进程执行,所有匹配到的数据混合后发送到该进程。如果匹配文本不以换行结束,会在发送前补加换行并在接收后去除。输入与输出数据按行对应,因此输入与输出的行数必须相同。

使用 --discrete 选项时,会为每个匹配的文本区域单独调用命令。你可以通过以下命令分辨差异。

greple -Mtee cat -n -- copyright LICENSE
greple -Mtee cat -n -- copyright LICENSE --discrete

--discrete 选项一起使用时,输入与输出数据的行数不必相同。

OPTIONS

CONFIGURATION

模块参数可使用 Getopt::EX::Config 模块按以下语法设置:

greple -Mtee::config(discrete) ...
greple -Mtee::config(fillup,crmode) ...

这在与 shell 别名或模块文件结合时很有用。

可用参数有:discretebulkmodecrmodefillupsqueezeblocksnofork

FUNCTION CALL

您可以通过在命令名前加上 & 来调用 Perl 函数,而不是外部命令。

greple -Mtee '&App::ansifold::ansifold' -w40 -- ...

该函数在派生的子进程中执行,因此必须遵循以下要求:

可以使用任何完全限定的函数名:

greple -Mtee '&Your::Module::function' -- ...

如果模块尚未加载,将自动加载。

为方便起见,提供以下简短别名:

使用函数调用可避免每次调用都为外部进程派生所带来的开销,当与 --discrete 选项一起使用时,可显著提升性能。

LEGACIES

由于 greple 中已实现 --stretch-S)选项,--blocks 选项已不再需要。你只需进行如下操作即可。

greple -Mtee cat -n -- --all -SE foo

不推荐使用 --blocks,因为它将来可能被弃用。

WHY DO NOT USE TEIP

首先,只要可以用 teip 命令做到,就用它。它是一个优秀且比 greple 快得多的工具。

由于 greple 旨在处理文档文件,它具有许多适用于此目的的功能,例如匹配区域控制。为了利用这些功能,使用 greple 也许是值得的。

此外,teip 不能将多行数据作为一个单元处理,而 greple 可以对由多行组成的数据块执行单独的命令。

EXAMPLE

下面的命令将会在包含于 Perl 模块文件中的 perlpod(1) 风格文档里查找文本区块。

greple --inside '^=(?s:.*?)(^=cut|\z)' --re '^([\w\pP].+\n)+' tee.pm

你可以将上面的命令与调用 deepl 命令的 -Mtee 模块组合执行,通过 DeepL 服务翻译它们,如下所示:

greple -Mtee deepl text --to JA - -- --fillup ...

不过,专用模块 App::Greple::xlate::deepl 在此目的上更有效。实际上,tee 模块的实现提示来自 xlate 模块。

EXAMPLE 2

下面的命令将在 LICENSE 文档中找到一些缩进部分。

greple --re '^[ ]{2}[a-z][)] .+\n([ ]{5}.+\n)*' -C LICENSE

  a) distribute a Standard Version of the executables and library files,
     together with instructions (in the manual page or equivalent) on where to
     get the Standard Version.

  b) accompany the distribution with the machine-readable source of the Package
     with your modifications.

你可以用带有 ansifold 命令的 tee 模块来重新格式化这部分。两个 --crmode 选项一起使用可以高效处理多行区块:

greple -Mtee ansifold -sw40 --prefix '     ' --crmode -- --crmode --re ...

  a) distribute a Standard Version of
     the executables and library files,
     together with instructions (in the
     manual page or equivalent) on where
     to get the Standard Version.

  b) accompany the distribution with the
     machine-readable source of the
     Package with your modifications.

--discrete 选项也可以使用,但会启动多个进程,因此执行时间更长。

EXAMPLE 3

考虑一种情况:你想从非表头行中 grep 字符串。例如,你可能想从 docker image ls 命令中搜索 Docker 镜像名称,但保留表头行。可以用下面的命令实现。

greple -Mtee grep perl -- -ML 2: --discrete --all

选项 -ML 2: 取出从第二行到倒数第一行的内容,并将其发送到 grep perl 命令。由于输入与输出的行数发生变化,需要使用 --discrete 选项,但因为命令只执行一次,所以没有性能上的劣势。

如果尝试用 teip 命令做同样的事情,teip -l 2- -- grep 会报错,因为输出行数少于输入行数。然而,得到的结果本身并没有问题。

INSTALL

CPANMINUS

$ cpanm App::Greple::tee

SEE ALSO

App::Greple::tee, https://github.com/kaz-utashiro/App-Greple-tee

https://github.com/greymd/teip

App::Greple, https://github.com/kaz-utashiro/greple

https://github.com/tecolicom/Greple

App::Greple::xlate

App::ansifold, https://github.com/tecolicom/App-ansifold

AUTHOR

Kazumasa Utashiro

LICENSE

Copyright © 2023-2026 Kazumasa Utashiro.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.