免费注册 查看新帖 |

Chinaunix

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

[文本处理] awk的RS怎样使用正则? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-14 11:14 |只看该作者 |倒序浏览
以下日志,想用日期时间所在的行作为RS分割,怎么弄呢?
  1. [oracle@test ~]$ cat test.log
  2. Sat Aug 31 08:26:44 2013
  3. Shutting down instance (abort)
  4. Sat Aug 31 13:11:09 2013
  5. Shutting down instance (abort)
  6. Sat Aug 31 13:23:10 2013
  7. Shutting down instance (abort)
  8. Sat Aug 31 13:26:12 2013
  9. Shutting down instance (abort)
  10. Tue Nov  5 13:23:43 CST 2013
  11. ORA-00001
  12. Tue Nov  6 13:23:43 CST 2013
  13. this is test
  14. ORA-00904
  15. Tue Nov  6 13:23:43 CST 2013
  16. ok,you win
  17. ORA-04031
  18. [oracle@source ~]$ awk 'BEGIN{RS=".*Nov.*\n"}{print NR,NF}' test.log
  19. 1 0
复制代码

论坛徽章:
771
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
2 [报告]
发表于 2014-02-14 11:18 |只看该作者
有时候我也想要类似的功能,但好像不支持?

论坛徽章:
0
3 [报告]
发表于 2014-02-14 11:31 |只看该作者
回复 2# Herowinter

可以支持呢,我没弄好:
  1. [oracle@test ~]$ awk 'BEGIN{RS="[^\n]*Nov[^\n]*"}{print NR,NF}' test.log
  2. 1 36
  3. 2 1
  4. 3 4
  5. 4 3
复制代码

论坛徽章:
771
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
4 [报告]
发表于 2014-02-14 11:42 |只看该作者
回复 3# 网中淫
恩,是可以,那好像你自己已经解决问题了?

   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
5 [报告]
发表于 2014-02-14 11:53 |只看该作者
回复 2# Herowinter

The RS is support with RE(Regular Express) in awk(GNU Awk 3.1.8)
and you can check it with RT variable

$ man awk
# search RS by /RS and get below:

   Records
       Normally, records are separated by newline characters.  You can control
       how records are separated by assigning values to the built-in  variable
       RS.   If  RS is any single character, that character separates records.
       Otherwise, RS is a regular expression.  Text in the input that  matches
       this  regular expression separates the record.  However, in compatibil‐
       ity mode, only the first character of its string value is used for sep‐
       arating  records.   If  RS  is set to the null string, then records are
       separated by blank lines.  When RS is set to the null string, the  new‐
       line  character  always acts as a field separator, in addition to what‐
       ever value FS may have.

       RT          The record terminator.  Gawk sets RT to the input text that
                   matched  the  character  or regular expression specified by
                   RS.

   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
6 [报告]
发表于 2014-02-14 11:57 |只看该作者
本帖最后由 jason680 于 2014-02-14 11:59 编辑

回复 3# 网中淫

you also can use RT variable to check your RS pattern

$ awk 'BEGIN{RS="[^\n]*Nov[^\n]*";OFS=","}{print NR,NF,RT}' test.log
1,36,Tue Nov  5 13:23:43 CST 2013
2,1,Tue Nov  6 13:23:43 CST 2013
3,4,Tue Nov  6 13:23:43 CST 2013
4,3,
   

论坛徽章:
1
综合交流区版块每日发帖之星
日期:2016-02-29 06:20:00
7 [报告]
发表于 2014-02-14 13:47 |只看该作者
manpage 确实需要细心的看看

RT这个功能不错~~

论坛徽章:
29
程序设计版块每日发帖之星
日期:2016-02-29 06:20:0015-16赛季CBA联赛之天津
日期:2016-08-10 10:33:1115-16赛季CBA联赛之深圳
日期:2016-08-17 15:07:2015-16赛季CBA联赛之佛山
日期:2016-11-07 11:33:5015-16赛季CBA联赛之广夏
日期:2016-11-15 09:13:31CU十四周年纪念徽章
日期:2016-11-24 14:12:25极客徽章
日期:2016-12-07 14:03:4015-16赛季CBA联赛之深圳
日期:2016-12-07 17:15:2715-16赛季CBA联赛之北京
日期:2016-12-22 09:30:0115-16赛季CBA联赛之深圳
日期:2016-12-22 10:49:2115-16赛季CBA联赛之山西
日期:2017-02-10 09:05:3215-16赛季CBA联赛之同曦
日期:2017-02-27 14:19:08
8 [报告]
发表于 2014-02-15 00:15 |只看该作者
  1. awk -vRS="[^\n]+:[0-9]+:[^\n]+" '{print NR,NF,RT}' RS  
  2. 1 0 Sat Aug 31 08:26:44 2013
  3. 2 4 Sat Aug 31 13:11:09 2013
  4. 3 4 Sat Aug 31 13:23:10 2013
  5. 4 4 Sat Aug 31 13:26:12 2013
  6. 5 4 Tue Nov  5 13:23:43 CST 2013
  7. 6 1 Tue Nov  6 13:23:43 CST 2013
  8. 7 4 Tue Nov  6 13:23:43 CST 2013
  9. 8 3
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP