分页: 1 / 1

awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:02
谢宝良
我想在被匹配的字符串后面插入一个字符。

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:15
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.

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:30
谢宝良
我想把形如 :数字数字数字数字: 变成:数字数字mm数字数字:
比如 :1205: 变成 :12mm05:
英文很差。并且我是使用win下面的awk编写的。

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:31
谢宝良

代码: 全选

   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().)

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:32
谢宝良
这是win版本的说明

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 20:49
谢宝良
猜测 大概是着个了 "\\&"

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 22:29
astolia

代码: 全选

$ echo '1234' | awk '{print gensub(/([0-9]{2})([0-9]{2})/, "\\0 => \\1mm\\2", "g")}'
1234 => 12mm34

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-04 23:07
科学之子
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.
这是哪里的manpage?
我这里Debian Stretch "man awk"没搜到gensub,只搜到4楼的gsub,介绍也基本(没仔细看)跟4楼一样

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-05 11:25
astolia
你需要安装gawk(GNU awk),它有若干非常有用的扩展功能,要用awk的话都推荐用这个版本。ubuntu以前默认安装的是mawk,功能比较弱。debian估计也是

Re: awk如何表示被查找到的字符串?

发表于 : 2017-09-07 8:47
谢宝良
试过了,window版本的awk,用\& 表示被找到的字符串。