Ash*_*har 1 sed command text-formatting
下面的 sed 命令有助于将文本添加Hello
到输入的每一行的末尾
<complex_query> | sed "s,$,Hello,"
Output:
myvar1: Hello
myvar2: Hello
Run Code Online (Sandbox Code Playgroud)
我现在希望用 的输出grep wow data.txt | cut -d: -f2
代替Hello
这怎么可能?
下面是我尝试过但不起作用的东西。
<complex_query> | sed "s,$,(grep wow data.txt | cut -d: -f2),"
Run Code Online (Sandbox Code Playgroud)
预期的期望输出:
myvar1: <output of grep wow data.txt | cut -d: -f2 command>
myvar2: <output of grep wow data.txt | cut -d: -f2 command>
Run Code Online (Sandbox Code Playgroud)
要在另一个命令的参数中使用命令的输出(减去尾随换行符),您需要使用命令替换,这是 Bourne shell 在 70 年代末引入的功能。
\n在 Bourne shell 中,语法是`cmd...`
. 也是如此csh
。
在现代sh
和sh
类似 shell 中,例如ksh
(来自)\xc2\xb9, zsh
, bash
,ash
基于 shell,语法是$(...)
相反的(尽管它们也支持 Bourne 语法以实现向后兼容性)。
在rc
-like shell 中,语法是`{cmd...}
在fish
shell 中,它不能(cmd ...)
在双引号内使用。从3.4.0版本开始,fish
还支持$(cmd...)
可以在双引号内使用which。
命令替换通常会以不同 shell 之间不同的方式分割命令的输出,从而导致传递给命令的多个参数,有些(包括大多数类似 Bourne 的 shell)甚至对结果单词执行通配(!)。通常可以通过将命令替换放在双引号内(而不是rc
像 shells\xc2\xb2 或fish
< 3.4.0\xc2\xb3 中)来防止这种情况。
所以在这里:
\n类似 POSIX 的 shell 或fish
3.4.0+:
<complex_query> | sed "s,\\$,$(grep wow data.txt | cut -d: -f2),"\n
Run Code Online (Sandbox Code Playgroud)\nrc
- 类似贝壳:
nl = \'\n\'\n<complex_query> | sed \'s,$,\'``($nl){grep wow data.txt | cut -d: -f2},\n
Run Code Online (Sandbox Code Playgroud)\ncsh
/ tcsh
:
<complex_query> | sed "s,\\$,`grep wow data.txt | cut -d: -f2`,"\n
Run Code Online (Sandbox Code Playgroud)\nfish
< 3.4.0
<complex_query> | sed \'s,$,\'(grep wow data.txt | cut -d: -f2 | string collect),\n
Run Code Online (Sandbox Code Playgroud)\n现在,请注意&
和字符在\命令\\
的替换部分中是特殊的,因此必须与 with (此处使用的分隔符)和换行符(如果存在)一起转义(如果可以存在换行符,请注意/中的拆分)如上所述)。sed
s
\\
,
csh
rc
请参阅如何确保插入到“sed”替换中的字符串转义所有相关元字符。
\n另一种方法是使用perl
而sed
不用担心特殊字符:
<complex_query> | REPL="$(cmd...)" perl -lpe \'$_ .= $ENV{REPL}\'\n
Run Code Online (Sandbox Code Playgroud)\n(这里假设有一个类似 POSIX 的 shell)
\n或者甚至让 perl
收集cmd
\ 的输出本身(使用它自己的`...`
,并修剪一个尾随换行符(如果有的话chomp
))。
<complex_query> | perl -lpe \'BEGIN{chomp ($repl = `cmd...`)}; $_ .= $repl\'\n
Run Code Online (Sandbox Code Playgroud)\n\xc2\xb9ksh93
和最新版本mksh
还支持一种${ ...; }
与其他形式不同的形式,但与 in 一样,fish
不引入子 shell 环境。
\xc2\xb2rc
之类的 shell 只有一种类型的引号:\'...\'
即强引号(内部不能发生扩展)。您可以通过使用``(){cmd ...}
(使用``(sep1 sep2){cmd ...}
指定用于拆分的分隔符列表的语法,此处为空)来防止拆分,但这也会防止尾随换行符修剪。
\xc2\xb3 还请注意,在 csh/tcsh 中,"`cmd ...`"
仍然在换行符上分割。