开发者

What does \1 in sed do?

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开发者_JS百科/g'

I know that g does a global substitution but really can't understand what's going on within the context of the question.


Here's a simple example:

$ echo 'abcabcabc' | sed 's/\(ab\)c/\1/'
ababcabc
$ echo 'abcabcabc' | sed 's/\(ab\)c/\1/g'
ababab
$ echo 'abcabcabc' | sed 's/\(ab\)\(c\)/\1d\2/g'
abdcabdcabdc

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.

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.

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


\(...\) would capture the characters specified inside of the parens and \1 would be used to reference the first match, this is a part of regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜