免费注册 查看新帖 |

Chinaunix

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

[文本处理] awk匹配到的多个范围选择问题(老帖) [复制链接]

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-29 18:23 |只看该作者 |倒序浏览
本帖最后由 tank064 于 2012-12-29 18:23 编辑
  1. cat gggg
  2. 1
  3. 2
  4. 3
  5. aaa
  6. 4
  7. 5
  8. bbb
  9. 6
  10. 7
  11. ccc
  12. 1
  13. ddd
  14. 3
  15. eee
  16. 2
  17. fff
  18. 6
  19. 本来想匹配 1 与 3 之间的行
  20. awk '/1/,/3/' gggg
  21. 1
  22. 2
  23. 3
  24. 1
  25. ddd
  26. 3

  27. 会把匹配到的行都显示处来,但只想要特定的行.google后,查到下面这句:
  28. awk '{t=t RS $0}END{print gensub(/.*(1.*3).*/,"\\1",1,t)}' ggggg
  29. 1
  30. ddd
  31. 3
复制代码
这里的{t=t RS $0} 是什么意思,怎么用的?

t=t RS $0  即t=t+RS+$0   把字符串连接起来后赋值?
还有:
gensub(/.*(1.*3).*/,"\\1",1,t)     t前面这个1是做什么用的?  man awk
  1.        gsub(r, s [, t])        For  each  substring  matching the regular expression r in the string t, substitute the
  2.                                string s, and return the number of substitutions.  If t is not supplied, use $0.  An  &
  3.                                in the replacement text is replaced with the text that was actually matched.  Use \& to
  4.                                get a literal &.  (This must be typed as "\\&"; see GAWK: Effective AWK Programming for
  5.                                a  fuller  discussion  of  the rules for &’s and backslashes in the replacement text of
  6.                                sub(), gsub(), and gensub().)
复制代码
没看到这个的说明.

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
2 [报告]
发表于 2012-12-29 18:30 |只看该作者
  1.        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
  2.                                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
  3.                                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
  4.                                that matched the n’th parenthesized subexpression.  The sequence \0 represents the entire matched text, as does the character &.
  5.                                Unlike sub() and gsub(), the modified string is returned as the result of the function, and the original target  string  is  not
  6.                                changed.
复制代码
指的是第一次匹配到的内容

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
3 [报告]
发表于 2012-12-29 18:53 |只看该作者
回复 1# tank064


    你具体是想要什么?

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
4 [报告]
发表于 2012-12-29 21:36 |只看该作者
本帖最后由 tank064 于 2012-12-29 21:37 编辑

回复 3# blackold
回黑哥:

就是 awk '/line1/,/line2/{print}'    file   这样会把所有的line1 到line2  之间所有的行都显示出来,但如果file中line1 line2 line1 line2  有多个这样的范围,怎么才能指定匹配具体哪个范围呢?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
5 [报告]
发表于 2012-12-30 09:49 |只看该作者
回复 4# tank064

你能用汉语描述"特定范围"的特征么?如果能,为什么不描述清楚?如果不能,那么用计算机语言就能描述清楚了?

   

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2012-12-30 11:21 |只看该作者
t=t RS $0  即t=t+RS+$0   把字符串连接起来后赋值?

是的,实际上它是把整个文件的内容都赋值给变量t,用以在END里面的gensub做替换。自己print一下就知道了:
  1. awk '{t=t RS $0}END{print t}' urfile
复制代码

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
7 [报告]
发表于 2012-12-30 11:22 |只看该作者
gensub(/.*(1.*3).*/,"\\1",1,t)     t前面这个1是做什么用的?  man awk

你应该man gawk

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
8 [报告]
发表于 2012-12-30 11:25 |只看该作者
回复 4# tank064


    你google到的那个代码,它也仅仅是利用的正则表达式的贪婪性来匹配最后一个1到3之间的内容而已。

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
9 [报告]
发表于 2013-01-04 10:42 |只看该作者
回复 5# waker
是这样的,假如一个文本是这样:

line1xxxx
......
line2xxxx
......
line1xxxx
......
line2xxxx
......
line1xxxx
......
line2xxxx
......

这段文本中有 三段line1xxxx  到 line2 xxxx 这样的行范围匹配. /line1xxxx/,/line2xxxx/ 这样会从第一个范围的line1xxxx 所在行 匹配到 最后一个范围的line2xxxx所在行,
也许这并不是我所希望得到的结果.  如果我这是想匹配到地一个 line1xxxx 到lie2xxxx的 范围 或是第二个,亦或是第三个时,  这时该如何匹配?


   

论坛徽章:
1
2015亚冠之西悉尼流浪者
日期:2015-05-28 16:30:37
10 [报告]
发表于 2013-01-04 10:45 |只看该作者
回复 3# blackold

是这样的,假如一个文本是这样:

line1xxxx
......
line2xxxx
......
line1xxxx
......
line2xxxx
......
line1xxxx
......
line2xxxx
......

这段文本中有 三段line1xxxx  到 line2 xxxx 这样的行范围匹配. /line1xxxx/,/line2xxxx/ 这样会从第一个范围的line1xxxx 所在行 匹配到 最后一个范围的line2xxxx所在行,
也许这并不是我所希望得到的结果.  如果我这是想匹配到地一个 line1xxxx 到lie2xxxx的 范围 或是第二个,亦或是第三个时,  这时该如何匹配?

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP