awk如何表示被查找到的字符串?
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
awk如何表示被查找到的字符串?
我想在被匹配的字符串后面插入一个字符。
- astolia
- 论坛版主
- 帖子: 6450
- 注册时间: 2008-09-18 13:11
Re: awk如何表示被查找到的字符串?
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.
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
Re: awk如何表示被查找到的字符串?
我想把形如 :数字数字数字数字: 变成:数字数字mm数字数字:
比如 :1205: 变成 :12mm05:
英文很差。并且我是使用win下面的awk编写的。
比如 :1205: 变成 :12mm05:
英文很差。并且我是使用win下面的awk编写的。
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
Re: awk如何表示被查找到的字符串?
代码: 全选
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().)
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
Re: awk如何表示被查找到的字符串?
这是win版本的说明
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
Re: awk如何表示被查找到的字符串?
猜测 大概是着个了 "\\&"
- astolia
- 论坛版主
- 帖子: 6450
- 注册时间: 2008-09-18 13:11
Re: awk如何表示被查找到的字符串?
代码: 全选
$ echo '1234' | awk '{print gensub(/([0-9]{2})([0-9]{2})/, "\\0 => \\1mm\\2", "g")}'
1234 => 12mm34
-
- 帖子: 2284
- 注册时间: 2013-05-26 6:58
- 系统: Debian 9
Re: awk如何表示被查找到的字符串?
这是哪里的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.
我这里Debian Stretch "man awk"没搜到gensub,只搜到4楼的gsub,介绍也基本(没仔细看)跟4楼一样
- astolia
- 论坛版主
- 帖子: 6450
- 注册时间: 2008-09-18 13:11
Re: awk如何表示被查找到的字符串?
你需要安装gawk(GNU awk),它有若干非常有用的扩展功能,要用awk的话都推荐用这个版本。ubuntu以前默认安装的是mawk,功能比较弱。debian估计也是
-
- 帖子: 1983
- 注册时间: 2010-05-01 21:23
Re: awk如何表示被查找到的字符串?
试过了,window版本的awk,用\& 表示被找到的字符串。