免费注册 查看新帖 |

Chinaunix

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

取一个段落中关键字的问题(已解决) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-29 08:29 |只看该作者 |倒序浏览
假如每行第2、5个元素要取,可以用awk '{print $2" "$5}'
假如有一些段落,每个段落我要取第2、5行应该如何处理呢?

论坛徽章:
0
2 [报告]
发表于 2005-04-29 09:03 |只看该作者

取一个段落中关键字的问题(已解决)

sed -n '2,5p' text|awk '{print $2" "$5}'
可不可以哦

论坛徽章:
0
3 [报告]
发表于 2005-04-29 09:07 |只看该作者

取一个段落中关键字的问题(已解决)

但是,消息是循环的
比如我要要看的是dhcpd.leases,显示其中的IP和MAC
原始资料如下
[root@CRIFST dhcp]# cat dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.1

lease 192.168.103.198 {
  starts 5 2005/04/29 00:55:41;
  ends 2 2038/01/19 03:14:06;
  tstp 2 2038/01/19 03:14:06;
  binding state active;
  next binding state free;
  hardware ethernet 00:30:05:06:af:14;
  uid "\001\0000\005\006\257\024";
  client-hostname "secend";
}
lease 192.168.103.193 {
  starts 5 2005/04/29 00:58:07;
  ends 2 2038/01/19 03:14:06;
  tstp 2 2038/01/19 03:14:06;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:6e:55:f7:dd;
  uid "\001\000\014nU\367\335";
  client-hostname "gaohangjun";
}
lease 192.168.103.192 {
  starts 5 2005/04/29 00:59:00;
  ends 2 2038/01/19 03:14:06;
  binding state active;
  next binding state free;
  hardware ethernet 00:30:05:03:8a:08;
  uid "\001\0000\005\003\212\010";
  client-hostname "jj";
}
lease 192.168.102.199 {
  starts 5 2005/04/29 00:59:46;
  ends 2 2038/01/19 03:14:06;
  binding state active;
  next binding state free;
  hardware ethernet 00:0f:3d:21:8d:2a;
  uid "\001\000\017=!\215*";
  client-hostname "DLA502";
}
lease 192.168.102.199 {
  starts 5 2005/04/29 00:59:46;
  ends 5 2005/04/29 01:01:16;
  tstp 5 2005/04/29 01:01:16;
  binding state free;
  hardware ethernet 00:0f:3d:21:8d:2a;
  uid "\001\000\017=!\215*";
}

只显示每段的 lease 和 hardware 那两行中的IP和MAC,并合并为一行输出

论坛徽章:
0
4 [报告]
发表于 2005-04-29 09:21 |只看该作者

取一个段落中关键字的问题(已解决)

目前已经用获得了这样的数据
[root@CRIFST dhcp]# grep -E "{|hard" dhcpd.leases
lease 192.168.103.198 {
  hardware ethernet 00:30:05:06:af:14;
lease 192.168.103.193 {
  hardware ethernet 00:0c:6e:55:f7:dd;
lease 192.168.103.192 {
  hardware ethernet 00:30:05:03:8a:08;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;
lease 192.168.102.199 {
  hardware ethernet 00:0f:3d:21:8d:2a;

论坛徽章:
0
5 [报告]
发表于 2005-04-29 09:36 |只看该作者

取一个段落中关键字的问题(已解决)

现有一个方法

  1. grep -E "{|hardware" dhcpd.leases|awk '{if(NR%2!=0)ORS="";else ORS="\n";print}'|awk '{print $2,$6}'|sed 's/;//g'|sort
复制代码


执行结果
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.200 00:e0:4c:e4:ff:26
192.168.102.200 00:e0:4c:e4:ff:26
192.168.103.192 00:30:05:03:8a:08
192.168.103.193 00:0c:6e:55:f7:dd
192.168.103.198 00:30:05:06:af:14

但有时候有问题
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.200 00:e0:4c:e4:ff:26
192.168.102.200 00:e0:4c:e4:ff:26
192.168.103.192 00:30:05:03:8a:08
192.168.103.193 00:0c:6e:55:f7:dd
192.168.103.194
192.168.103.194 00:11:5b:22:8f:ae
192.168.103.195
192.168.103.196
192.168.103.196 00:11:5b:22:8f:ae
192.168.103.197
192.168.103.198 00:30:05:06:af:14
192.168.103.200
ethernet
ethernet
ethernet
ethernet
ethernet
ethernet
ethernet


有问题的部分原始资料
lease 192.168.103.200 {
  starts 5 2005/04/29 01:37:54;
  ends 5 2005/04/29 01:37:54;
  binding state free;
  client-hostname "lenovo-2f31e43d";
}
lease 192.168.103.197 {
  starts 5 2005/04/29 01:38:27;
  ends 5 2005/04/29 01:38:27;
  tstp 5 2005/04/29 01:38:27;
  binding state free;
}
lease 192.168.103.196 {
  starts 5 2005/04/29 01:38:38;
  ends 5 2005/04/29 01:38:38;
  tstp 5 2005/04/29 01:38:38;
  binding state free;
}
lease 192.168.103.195 {
  starts 5 2005/04/29 01:38:49;
  ends 5 2005/04/29 01:38:49;
  tstp 5 2005/04/29 01:38:49;
  binding state free;
}
lease 192.168.103.194 {
  starts 5 2005/04/29 01:39:00;
  ends 5 2005/04/29 01:39:00;
  tstp 5 2005/04/29 01:39:00;
  binding state free;
}
lease 192.168.102.197 {
  starts 5 2005/04/29 01:38:27;
  ends 5 2005/04/29 01:39:57;
  tstp 5 2005/04/29 01:39:57;
  binding state free;
  hardware ethernet 00:0f:3d:21:8d:30;
  uid "\001\000\017=!\2150";
}

论坛徽章:
0
6 [报告]
发表于 2005-04-29 10:09 |只看该作者

取一个段落中关键字的问题(已解决)

awk '/\{|(hardware)/' bb.txt|tr -d  '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//'
我提供一种方法,你试验一下!
ps:我在tru64,ksh下是通过了。
看了你的有问题的资料,上面的是不合适的

论坛徽章:
0
7 [报告]
发表于 2005-04-29 10:13 |只看该作者

取一个段落中关键字的问题(已解决)

试验结果如下
[root@CRIFST dhcp]# awk '/\{|(hardware)/' dhcpd.leases|tr -d  '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//'
192.168.103.198-00:30:05:06:af:14
192.168.103.193-00:0c:6e:55:f7:dd
192.168.103.192-00:30:05:03:8a:08
192.168.102.200-00:e0:4c:e4:ff:26
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.200-00:e0:4c:e4:ff:26
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.197-00:0f:3d:21:8d:30
192.168.103.200-192.168.103.199-00:0d:87:4e:4a:ac
192.168.102.199-00:0f:3d:21:8d:2a
192.168.103.197-00:11:5b:22:8f:ae
192.168.102.197-00:0f:3d:21:8d:30
192.168.103.197-192.168.103.196-00:11:5b:22:8f:ae
192.168.103.196-192.168.103.195-00:11:5b:22:8f:ae
192.168.103.195-192.168.103.194-00:11:5b:22:8f:ae
192.168.103.194-192.168.103.191-00:11:5b:22:8f:ae
192.168.102.199-00:0f:3d:21:8d:2a
192.168.103.200-192.168.103.197-192.168.103.196-192.168.103.195-192.168.103.194-192.168.102.197-00:0f:3d:21:8d:30
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.197-00:0f:3d:21:8d:30
192.168.103.199-00:0d:87:4e:4a:ac
192.168.102.199-00:0f:3d:21:8d:2a
192.168.103.200-00:00:00:00:00:00
192.168.103.190-00:00:00:00:00:00
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.197-00:0f:3d:21:8d:30
192.168.102.199-00:0f:3d:21:8d:2a
192.168.102.197-00:0f:3d:21:8d:30

论坛徽章:
0
8 [报告]
发表于 2005-04-29 10:37 |只看该作者

取一个段落中关键字的问题(已解决)


  1. cat dhcpd.leases|awk 'BEGIN{ORS=RS="}"}/hard/&&/lease/{print}'|grep -E "{|hardware"|tr "\n" " "|sed 's/lease//g;s/lease//g;s/hardware ethernet//g;s/{//g;s/;//g'|xargs -n2|sort|uniq -c|awk '{print $2" "$3}'
复制代码

  1. cat dhcpd.leases|awk 'BEGIN{ORS=RS="}"}/hard/&&/lease/{print}'|grep -E "{|hardware"|awk '{if(NR%2!=0)ORS="";else ORS="\n";print}'|awk '{print $2,$6}'|sed 's/;//g'|sort|uniq -c|awk '{print $2" "$3}'
复制代码

两个都可以,这样就完美了,不过太长了

论坛徽章:
0
9 [报告]
发表于 2005-04-29 10:59 |只看该作者

取一个段落中关键字的问题(已解决)

试试这个:
nawk '{
    gsub(";",""
    if($0~/lease/)
         printf("%s ", $2)
    if($0~/hardware/)
        printf("%s\n", $3)
}' filename

在unix下通过。

论坛徽章:
0
10 [报告]
发表于 2005-04-29 11:12 |只看该作者

取一个段落中关键字的问题(已解决)

原帖由 "begincwcw" 发表:
试试这个:
nawk '{
    gsub(";",""
    if($0~/lease/)
         printf("%s ", $2)
    if($0~/hardware/)
        printf("%s\n", $3)
}' filename

在unix下通过。

我没有nawk,用awk替换,测试结果:失败
效果和
awk '/\{|(hardware)/' bb.txt|tr -d  '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//'
类似,只不过IP中间是空格,不是“-”
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP