awk如何表示被查找到的字符串?
发表于 : 2017-09-04 20:02
我想在被匹配的字符串后面插入一个字符。
代码: 全选
gensub(r, s, h [, t]) Search the target string t for matches of the
regular expression r. If h is a string begin‐
ning with g or G, then replace all matches of r
with s. Otherwise, h is a number indicating
which match of r to replace. If t is not sup‐
plied, use $0 instead. Within the replacement
text s, the sequence \n, where n is a digit
from 1 to 9, may be used to indicate just the
text that matched the n'th parenthesized subex‐
pression. The sequence \0 represents the
entire matched text, as does the character &.
Unlike sub() and gsub(), the modified string is
returned as the result of the function, and the
original target string is not changed.
代码: 全选
gsub(r, s [, t]) For each substring matching the
regular expression r in the
string t, substitute the string
s, and return the number of sub-
stitutions. If t is not sup-
plied, use $0. An & in the
replacement text is replaced
with the text that was actually
matched. Use \& to get a lit-
eral &. (This must be typed as
"\\&"; see GAWK: Effective AWK
Programming for a fuller discus-
sion of the rules for &'s and
backslashes in the replacement
text of sub(), gsub(), and gen-
sub().)
代码: 全选
$ echo '1234' | awk '{print gensub(/([0-9]{2})([0-9]{2})/, "\\0 => \\1mm\\2", "g")}'
1234 => 12mm34
这是哪里的manpage?astolia 写了:manpage 里写的很清楚
代码: 全选
gensub(r, s, h [, t]) Search the target string t for matches of the regular expression r. If h is a string begin‐ ning with g or G, then replace all matches of r with s. Otherwise, h is a number indicating which match of r to replace. If t is not sup‐ plied, use $0 instead. Within the replacement text s, the sequence \n, where n is a digit from 1 to 9, may be used to indicate just the text that matched the n'th parenthesized subex‐ pression. The sequence \0 represents the entire matched text, as does the character &. Unlike sub() and gsub(), the modified string is returned as the result of the function, and the original target string is not changed.