免费注册 查看新帖 |

Chinaunix

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

急,请教shell编程的一个问题。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-07 20:17 |只看该作者 |倒序浏览
这个是帮linux系统管理版块的一同学提的问题,请各位牛人帮忙,谢谢啦


我要根据host_name在下面的文件中提取一段内容。而shell脚本中是以回车或者空格为记录分隔符。但是我想要根据 host_name关键字(比如 mail )从文件中提取define 开始到 } 的内容要怎么做呢?
#define service {
#        host_name             136_stats
#        service_description   check_ssh
#        check_period          full_period
#        max_check_attempts    4
#        normal_check_interval 2
#        retry_check_interval  1
#        contact_groups        admins
#        notification_interval   10
#        notification_period     full_period
#        notification_options    u,c,r
#        check_command      check_ssh!-p 22 115.182.52.136
#        }


# Define a service to "check temp"

define service {
        host_name mail
        service_description check_temp
        check_period full_period
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period full_period
        notification_options u,c,r
        check_command nrpe!check_temp
        }

论坛徽章:
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
2 [报告]
发表于 2011-05-07 20:30 |只看该作者
awk -v RS=   '/ host_name mail/' urfile

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
3 [报告]
发表于 2011-05-07 20:34 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
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
4 [报告]
发表于 2011-05-07 20:49 |只看该作者
  1. awk '/.*define service {.*/{print ""}1' urfile | awk -v RS= '/ host_name mail/'
复制代码

论坛徽章:
0
5 [报告]
发表于 2011-05-07 20:51 |只看该作者
awk -v RS=   '/ host_name mail/' urfile
waker 发表于 2011-05-07 20:30



    嗯,多谢,那如果是这种情况呢
Define a service to "check temp"
define service {
        host_name mail
        service_description check_temp
        check_period full_period
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period full_period
        notification_options u,c,r
        check_command nrpe!check_temp
        }

论坛徽章:
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 [报告]
发表于 2011-05-07 20:52 |只看该作者
回复 5# shplpy


4楼的如何?

论坛徽章:
0
7 [报告]
发表于 2011-05-07 20:57 |只看该作者
Shell_HAT 发表于 2011-05-07 20:49



    哈哈,这个不错,但是没考虑这种情况
define service {
        host_name mail
        service_description check_temp
        check_period full_period
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period full_period
        notification_options u,c,r
        check_command nrpe!check_temp
        }
aaaaaaaaaaaaaaaa


还请赐教,多谢啦~

论坛徽章:
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 [报告]
发表于 2011-05-07 21:02 |只看该作者
回复 7# shplpy
  1. awk '{if($0~/{/)$0="\n"$0;else if($0~/}/)$0=$0"\n"}1' urfile | awk -v RS= '/ host_name mail/'
复制代码

论坛徽章:
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
9 [报告]
发表于 2011-05-07 21:06 |只看该作者
  1. awk '/{/{$0="\n"$0}/}/{$0=$0"\n"}1' urfile | awk -v RS= '/ host_name mail/'
复制代码

论坛徽章:
0
10 [报告]
发表于 2011-05-07 21:30 |只看该作者
Shell_HAT 发表于 2011-05-07 21:06



    多谢这位大哥!这回成了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP