免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2286 | 回复: 4
打印 上一主题 下一主题

gensub函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-13 17:00 |只看该作者 |倒序浏览
[root@Mylinux tmp]# awk '{print gensub(/[^0-9]*([0-9]+).*/,"\\1",1,$2),$0}' file
1234 xxxx   xxxx_1234_5678_xxxx.log  xxxx
2234 xxxx   xxxx_2234_xxxx.log          xxxx
3234 xxxx   3234_xxx_xxx.log             xxxx
4234 xxxx   xxxx_xxxx_4234.log          xxxx



ywlscpl
天使       兄弟  你老是用这个函数 。我的awk 里好像没有这个函数。
你来解释一下吧 。

[ 本帖最后由 greendays 于 2009-4-13 17:08 编辑 ]

评分

参与人数 1可用积分 -10 收起 理由
waker -10 好逸恶劳奖金

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2009-04-13 17:05 |只看该作者

回复 #1 greendays 的帖子

还得自己看看,别人讲也是看man,给你翻译一下,


gensub(r, s, h [, t])   Search the target string t for matches of the regular expression r.  If h is a string beginning 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 supplied, $0 is
                               used 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 subexpression.  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.

论坛徽章:
0
3 [报告]
发表于 2009-04-13 17:08 |只看该作者
gensub(a,b,c[,d])全局替换,匹配正则a,用b替换,c为指定替换目标是第几次匹配,d为指定替换目标是哪个域如$1,$2,若无d指$0,返回值为target替换后内容(未替换还是返回target原内容),与sub、gsub不同的是,target内容替换后不改变。

gensub(/123/,"x",1,$1)替换$1中 第一次匹配到的123为字符x,返回值为$1替换后的内容,且$1的内容并没有改变

gensub(/a(.*)b/,"\\1",1) 返回值为匹配正则第1对()内的内容
gensub(/a(.*)b(.*)c/,"\\2",1) 返回值为匹配正则第2对()内的内容

论坛徽章:
0
4 [报告]
发表于 2009-04-13 17:14 |只看该作者
waker斑斑 ,帖子重新编辑了,你把手指拿掉吧。

论坛徽章:
0
5 [报告]
发表于 2009-04-13 17:21 |只看该作者

回复 #3 ywlscpl 的帖子

谢谢 ywlscpl兄,解释的非常明白!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP