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

sh/bash/dash/ksh/zsh等Shell脚本
回复
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

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

#1

帖子 谢宝良 » 2017-09-04 20:02

我想在被匹配的字符串后面插入一个字符。
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

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

#2

帖子 astolia » 2017-09-04 20:15

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如何表示被查找到的字符串?

#3

帖子 谢宝良 » 2017-09-04 20:30

我想把形如 :数字数字数字数字: 变成:数字数字mm数字数字:
比如 :1205: 变成 :12mm05:
英文很差。并且我是使用win下面的awk编写的。
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

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

#4

帖子 谢宝良 » 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().)
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

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

#5

帖子 谢宝良 » 2017-09-04 20:32

这是win版本的说明
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

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

#6

帖子 谢宝良 » 2017-09-04 20:49

猜测 大概是着个了 "\\&"
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

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

#7

帖子 astolia » 2017-09-04 22:29

代码: 全选

$ 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如何表示被查找到的字符串?

#8

帖子 科学之子 » 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楼一样
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

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

#9

帖子 astolia » 2017-09-05 11:25

你需要安装gawk(GNU awk),它有若干非常有用的扩展功能,要用awk的话都推荐用这个版本。ubuntu以前默认安装的是mawk,功能比较弱。debian估计也是
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

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

#10

帖子 谢宝良 » 2017-09-07 8:47

试过了,window版本的awk,用\& 表示被找到的字符串。
回复