免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3121 | 回复: 14

[文本处理] 正则提取匹配行的单词 [复制链接]

论坛徽章:
0
发表于 2014-07-11 11:26 |显示全部楼层
ltm profile httpclass httpclass_p2pd
    asm disabled
    pool pool_p2pd


我的需求是匹配 ltm 行的我要提取 httpclass单词 ,匹配pool行的我要提取 pool_p2pd单词,最后打印在一行里面,如下

httpclass  pool_p2pd

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
发表于 2014-07-11 11:29 |显示全部楼层
本帖最后由 seesea2517 于 2014-07-11 11:30 编辑

回复 1# hchfvt
  1. [seesea@UC ~]$ cat file
  2. ltm profile httpclass httpclass_p2pd
  3.     asm disabled
  4.     pool pool_p2pd
  5. [seesea@UC ~]$ awk '/ltm/{printf $3 " "} /pool/{printf $2}' file
  6. httpclass pool_p2pd
复制代码
这样做太弱了点会不会,期待高人。

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
发表于 2014-07-11 11:32 |显示全部楼层
匹配规则是什么?

论坛徽章:
0
发表于 2014-07-11 11:36 |显示全部楼层
回复 2# seesea2517

对不起,是我没表达清楚,,我有很多行内容,
ltm profile httpclass httpclass_p2pd
    asm disabled
    pool pool_p2pd
ltm profile httpclass httpclass_p3pd
    asm disabled
    pool pool_p3pd
ltm profile httpclass httpclass_p4pd
    asm disabled
    pool pool_p4pd

希望打印出来如下
httpclass_p2pd pool_p2pd
httpclass_p3pd pool_p3pd
httpclass_p4pd pool_p4pd


   

论坛徽章:
0
发表于 2014-07-11 11:41 |显示全部楼层
回复 2# seesea2517
awk '/ltm/{printf $3 " "} /pool/{printf $2"\r\n"}' file

就可以了,,多谢!

   

论坛徽章:
0
发表于 2014-07-11 11:52 |显示全部楼层
回复 5# hchfvt

  1. 1064 11:51:24 root@localhost:~/shelltest/CU/4145569 # cat q
  2. ltm profile httpclass httpclass_p2pd
  3.     asm disabled
  4.     pool pool_p2pd
  5. ltm profile httpclass httpclass_p3pd
  6.     asm disabled
  7.     pool pool_p3pd
  8. ltm profile httpclass httpclass_p4pd
  9.     asm disabled
  10.     pool pool_p4pd
  11. 1065 11:51:25 root@localhost:~/shelltest/CU/4145569 # awk '/ltm/{printf $3FS}/pool/{print $2}' q
  12. httpclass pool_p2pd
  13. httpclass pool_p3pd
  14. httpclass pool_p4pd
  15. 1066 11:51:26 root@localhost:~/shelltest/CU/4145569 #
复制代码
略微简化了一点。

论坛徽章:
0
发表于 2014-07-11 12:36 |显示全部楼层
回复 6# 用户名注册后不能更改


[root@ # cat test.txt
ass httpclass_dams {
    asm disabled
    defaults-from httpclass
     pool pool_dams
    redirect none
}
ltm profile httpclass httpclass_damsbase {
    asm disabled
    defaults-from httpclass
    pool pool_damsbase
    redirect none
}
ltm profile httpclass httpclass_damsstan {
    asm disabled
    defaults-from httpclass
    pool pool_damsstan
    redirect none
}
ltm profile httpclass httpclass_p2pd {
    asm disabled
    defaults-from httpclass
    pool pool_p2pd
    redirect none
[root@ # cat test.txt | awk '/ltm/{printf $3FS} /asm/{print $2FS} /pool/{printf $2"\r\n"}'
disabled
pool_dams
httpclass disabled
pool_damsbase
httpclass disabled
pool_damsstan
httpclass disabled
pool_p2pd



得出的结果如上面的输出,我希望得出的结果如下:
httpclass disabled pool_damsbase
httpclass disabled pool_p2pd

还请高手帮忙看看 ,,多谢!

论坛徽章:
6
处女座
日期:2014-04-02 16:07:17酉鸡
日期:2014-04-14 10:09:22子鼠
日期:2014-04-17 11:57:30辰龙
日期:2014-09-01 17:14:08戌狗
日期:2014-10-28 12:25:54未羊
日期:2014-11-14 11:31:58
发表于 2014-07-11 12:46 |显示全部楼层
  1. awk '/\<ltm\>/{s=s" "gensub(/.*(httpclass_[^ ]+).*/,"\\1","g")} /pool/{s=s" "gensub(/.*(pool_[^ ]+).*/,"\\1","g")"\n"} END{print s}' oo
复制代码
我猜

论坛徽章:
766
金牛座
日期: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
发表于 2014-07-11 12:48 |显示全部楼层
回复 7# hchfvt
  1. awk '/ltm/{printf $3FS} /asm/{printf $2FS} /pool/{printf $2"\r\n"}' test.txt
复制代码

论坛徽章:
0
发表于 2014-07-11 12:52 |显示全部楼层
回复 7# hchfvt

  1. 1087 12:47:29 root@localhost:~/shelltest/CU/4145569 # cat qq
  2. ass httpclass_dams {
  3.     asm disabled
  4.     defaults-from httpclass
  5.      pool pool_dams
  6.     redirect none
  7. }
  8. ltm profile httpclass httpclass_damsbase {
  9.     asm disabled
  10.     defaults-from httpclass
  11.     pool pool_damsbase
  12.     redirect none
  13. }
  14. ltm profile httpclass httpclass_damsstan {
  15.     asm disabled
  16.     defaults-from httpclass
  17.     pool pool_damsstan
  18.     redirect none
  19. }
  20. ltm profile httpclass httpclass_p2pd {
  21.     asm disabled
  22.     defaults-from httpclass
  23.     pool pool_p2pd
  24.     redirect none
  25. 1088 12:47:31 root@localhost:~/shelltest/CU/4145569 # awk '/ltm/{a=1;printf $3FS}/asm/&&a{printf $2FS}/pool/&&a{a=0;print $2}' qq
  26. httpclass disabled pool_damsbase
  27. httpclass disabled pool_damsstan
  28. httpclass disabled pool_p2pd
  29. 1089 12:47:31 root@localhost:~/shelltest/CU/4145569 #
复制代码
print自带换行,printf不带。
a的作用是将几个条件关联起来,因为需求是整体都输出或都不输出。

话说,如果我没记错,ass是那个的意思吧……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP