sed 中的 1 有什么作用? [英] What does 1 in sed do?
问题描述
我发现这个问题与我想要的非常相关:使用 awk 或sed 在 Unix 中,但是我无法弄清楚以下内容的作用:
I found this question really relevant to what I wanted: Parsing using awk or sed in Unix, however I can't figure out what the following does:
's/([,=]) /1/g'
我知道 g 进行了全局替换,但真的无法理解在问题的上下文中发生了什么.
I know that g does a global substitution but really can't understand what's going on within the context of the question.
推荐答案
这是一个简单的例子:
$ echo 'abcabcabc' | sed 's/(ab)c/1/'
ababcabc
$ echo 'abcabcabc' | sed 's/(ab)c/1/g'
ababab
$ echo 'abcabcabc' | sed 's/(ab)(c)/1d2/g'
abdcabdcabdc
在第一个命令中,只影响第一个匹配项.在第二个命令中,每个匹配项都会受到影响.在这两种情况下,1
指的是被转义括号捕获的字符.
In the first command, only the first match is affected. In the second command, every match is affected. In both cases, the 1
refers to the characters captured by the escaped parentheses.
在第三个命令中,指定了两个捕获组.它们通过使用 1
和 2
来引用.最多可以使用九个捕获组.
In the third command, two capture groups are specified. They are referred to by using 1
and 2
. Up to nine capture groups can be used.
除了 g
(全局)运算符(或没有它,第一个匹配项)之外,您还可以指定一个特定的匹配项:
In addition to the g
(global) operator (or without it, the first match), you can specify a particular match:
$ echo 'aaaaaa' | sed 's/a/A/4'
aaaAaa
这篇关于sed 中的 1 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!