sed做表达式替换的时候,可以使用&来表示前面正则表达式的匹配的部分。而且还可以用\1 ...\9来引用前面的子表达式的内容。
man sed可以看到如下解释:
s/regexp/replacement/
Attempt to match regexp against the pattern space. If success-
ful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in the regexp.
举个小例子:
[ying@localhost]$ echo "\"tom is a good boy\"" | sed "s#\".*\?#\"\"&\"\"#g"
"""tom is a good boy"""
man sed可以看到如下解释:
s/regexp/replacement/
Attempt to match regexp against the pattern space. If success-
ful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in the regexp.
举个小例子:
[ying@localhost]$ echo "\"tom is a good boy\"" | sed "s#\".*\?#\"\"&\"\"#g"
"""tom is a good boy"""