免费注册 查看新帖 |

Chinaunix

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

如何实现从一个文件选取特定的一些值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-01 09:39 |只看该作者 |倒序浏览
有一个文件hosts.cfg内容类似如下格式:
define host {
       host_name                    host1
       alias                              server1
       address                        192.168.0.1
       contact_groups             admin
       notification_interval      10
       notification_period        24x7
       notification_options      w,u,c,r

       check_command           check-host-alive
       }

define host {
       host_name                    host2
       alias                              server2
       address                        192.168.0.2
       contact_groups             admin
       notification_interval      10
       notification_period        24x7
       notification_options      d,u,r

       check_command           check-host-alive
       }

......

想要输入结果如下格式:
host1  192.168.0.1  w,u,c,r
host2  192.168.0.2  d,u,r
......

也就是分别取出host_name,address和notification_options的值放在一行里(用空格分隔),请问用sed or awk等工具如何实现,谢谢!

论坛徽章:
1
处女座
日期:2014-12-23 17:59:27
2 [报告]
发表于 2008-10-01 11:27 |只看该作者
awk '/host_name/||/address/||/notification_options/{print $2}' a |xargs -n3

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
3 [报告]
发表于 2008-10-01 11:34 |只看该作者

  1. awk '/define/&&/host/ {
  2.         a[1]=a[2]=a[3]="";
  3.         while(getline) {
  4.                 if($1~/host_name/)
  5.                         a[1]=$2;
  6.                 else if($1~/address/)
  7.                         a[2]=$2;
  8.                 else if($1~/notification_options/)
  9.                         a[3]=$2;
  10.                 else if($1~/}/) {
  11.                         print a[1],a[2],a[3]
  12.                         next;
  13.                 }
  14.         }
  15. }'  hosts.cfg

复制代码

论坛徽章:
0
4 [报告]
发表于 2008-10-01 11:38 |只看该作者
LZ在搞Nagios ................

hostname=awk '/host_name/{ print $2}'
address=awk '/address/{ print $2}'
notifiction_options=awk '/notification_options/{ print $2}'

论坛徽章:
0
5 [报告]
发表于 2008-10-01 12:41 |只看该作者
多谢楼上各位!这里高手真多。4楼的那个没写完吧?

论坛徽章:
0
6 [报告]
发表于 2008-10-01 16:36 |只看该作者
sed -nr '/host[0-9]+$|\.|,/!ba;s/.*\s(.*)$/\1/;H;:a;/}/{x;s/\n/ /g;s/^[^h]+//;p}'

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
7 [报告]
发表于 2008-10-01 19:37 |只看该作者
awk '{print $5,$9,$17}' RS=} hosts.cfg

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2008-10-02 01:28 |只看该作者
awk ‘BEGIN{RS=“}”}{print $5,$9,$17}' ur-file

论坛徽章:
0
9 [报告]
发表于 2008-10-02 16:21 |只看该作者
原帖由 dreammaker 于 2008-10-1 12:41 发表
多谢楼上各位!这里高手真多。4楼的那个没写完吧?


国庆没机器测试,不好意思,乱写了,补上

最士的方法:

awk '/host_name/{ printf("Hostname: %s" , $2) }/address/{ printf("IP_Address:%s" ,$2)}/options/{ printf("Notification_options:%s\n",$2)}' hosts.cfg

输出如下:
Hostname: host1 IP_Address:192.168.0.1 Notification_options:w,u,c,r
Hostname: host2 IP_Address:192.168.0.2 Notification_options:d,u,r

[ 本帖最后由 xwmhmily 于 2008-10-2 16:48 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP