Sed - 删除表达式需要大括号,而不是替代表达式
Sed - Curly braces needed for a delete expression, not for a substitute one
在 I answered these two sed :
第一个:
sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' \
-e 's/^delay=/ens_delay=/' your_file.txt
第二个:
sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' \
-e 's/^delay=/ens_delay=/' your_file.txt
在第二行中,我发现我需要在 /^delay=.*$/d
周围加上大括号才能使其正常工作。但是第一行中的 s/^delay=.*$//
不是必需的(尽管它适用于)。
为什么会有这种差异?
免责声明:答案是 Why are these curly braces necessary in sed?
的删减副本
根据 POSIX standard's page on sed
:
The script shall consist of editing commands of the following form:
[address[,address]]function
where function
represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.
所以地址后的第一个非空白字符被当作命令动词。
下面进一步引用了大括号:
[2addr] {editing command
editing command
...
}
Execute a list of `sed` editing commands only when the pattern space is selected. …
[2addr]表示允许的最大地址数为2个。
为了澄清上述观点,sed(1)
的 Addresses 部分说:
Sed
commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address.
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);
… (and other stuff not relevant to this discussion) …
gnu info page (info sed
) 与 {
和 }
的描述相似,
在“3.4 常用命令”下:
{ COMMANDS }
A group of commands may be enclosed
between {
and }
characters.
This is particularly useful when you want a group of commands to be triggered
by a single address (or address-range) match.
换句话说,大括号用于在同一地址应用多个命令或嵌套地址。
这里的标准不是很明确1 但左大括号 {
实际上是 启动一组其他 sed 命令的命令(该组以右大括号 }
结束)。
1:
不过,如果您阅读整个页面,则会提到:
除 {、a、b、 以外的命令动词c, i, r, t, w、: 和 # 后面可以跟...
在
第一个:
sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' \ -e 's/^delay=/ens_delay=/' your_file.txt
第二个:
sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' \ -e 's/^delay=/ens_delay=/' your_file.txt
在第二行中,我发现我需要在 /^delay=.*$/d
周围加上大括号才能使其正常工作。但是第一行中的 s/^delay=.*$//
不是必需的(尽管它适用于)。
为什么会有这种差异?
免责声明:答案是 Why are these curly braces necessary in sed?
的删减副本根据 POSIX standard's page on sed
:
The script shall consist of editing commands of the following form:
[address[,address]]functionwhere
function
represents a single-character command verb from the list in Editing Commands in sed, followed by any applicable arguments.
所以地址后的第一个非空白字符被当作命令动词。
下面进一步引用了大括号:
[2addr] {editing command editing command ... }Execute a list of `sed` editing commands only when the pattern space is selected. …
[2addr]表示允许的最大地址数为2个。
为了澄清上述观点,sed(1)
的 Addresses 部分说:
Sed
commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will be executed only for input lines which match that address; or with two addresses, in which case the command will be executed for all input lines which match the inclusive range of lines starting from the first address and continuing to the second address. Three things to note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);
… (and other stuff not relevant to this discussion) …
gnu info page (info sed
) 与 {
和 }
的描述相似,
在“3.4 常用命令”下:
{ COMMANDS }A group of commands may be enclosed between
{
and}
characters. This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match.
换句话说,大括号用于在同一地址应用多个命令或嵌套地址。
这里的标准不是很明确1 但左大括号 {
实际上是 启动一组其他 sed 命令的命令(该组以右大括号 }
结束)。
1:
不过,如果您阅读整个页面,则会提到:
除 {、a、b、 以外的命令动词c, i, r, t, w、: 和 # 后面可以跟...