免费注册 查看新帖 |

Chinaunix

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

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

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

论坛徽章:
0
2 [报告]
发表于 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
3 [报告]
发表于 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
4 [报告]
发表于 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
5 [报告]
发表于 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
6 [报告]
发表于 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
7 [报告]
发表于 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中间是空格,不是“-”

论坛徽章:
0
8 [报告]
发表于 2005-04-29 12:07 |显示全部楼层

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

烈火大哥,你又给俺捣乱,再弄点sleep不更好

论坛徽章:
0
9 [报告]
发表于 2005-04-29 16:02 |显示全部楼层

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

guangzongy
代码:

awk 'BEGIN{i=1}{if ($1=="lease" IP=$2;if ($1=="hardware"&&$3!="" {print IP"-"substr($3,1,length
($3)-1)}}' yourfile

执行结果如下
192.168.104.200-0
192.168.104.198-0
192.168.104.197-0
192.168.104.195-0
192.168.104.193-0
192.168.104.192-0
192.168.104.191-0
192.168.104.190-0
192.168.103.183-0
192.168.103.190-0
192.168.103.200-0
192.168.103.199-0
192.168.103.198-0
192.168.103.197-0
192.168.103.196-0
192.168.103.193-0
192.168.103.192-0
192.168.103.191-0
192.168.103.189-0
192.168.103.188-0
192.168.103.187-
192.168.103.186-0
192.168.103.185-0
192.168.103.184-0
192.168.103.182-0
192.168.102.200-0
192.168.102.199-0
192.168.102.198-0
192.168.102.197-0
192.168.102.196-0
192.168.102.195-0
192.168.101.200-0
192.168.101.197-0
192.168.101.196-0
192.168.101.195-0
192.168.101.194-0
192.168.101.193-0
192.168.101.192-0
192.168.101.102-0
192.168.103.195-0
192.168.103.194-0
192.168.103.181-0
192.168.103.181-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.103.199-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.102.197-0
192.168.103.199-0
192.168.102.197-0
192.168.102.197-0





begincwcw
我在unix下的运行结果和烈火兄的运行结果是一样的!!!  
#sh i

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.199 00:0f:3d:21:8d:2a
192.168.102.199 00:0f:3d:21:8d:2a

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

也许是gsub错,把""改为//试试。

改成//或者不改,结果一样,如下
not store The This 192.168.104.200 00:10:5c:f6:6c:10
192.168.104.198 00:0f:ea:b4:cb:82
192.168.104.197 00:0f:ea:b4:bd:e6
192.168.104.195 00:0f:ea:b3:82:fb
192.168.104.193 00:0f:ea:b2:ac:8b
192.168.104.192 00:0f:ea:b4:cb:32
192.168.104.191 00:04:23:8c:a4:6f
192.168.104.190 08:00:46:b7:dd:f8
192.168.103.183 00:0a:e4:2e:37:e9
192.168.103.190 00:00:00:00:00:00
192.168.103.200 00:00:00:00:00:00
192.168.103.199 00:0d:87:4e:4a:ac
192.168.103.198 00:30:05:06:af:14
192.168.103.197 00:0a:e4:2e:37:e9
192.168.103.196 00:0f:3d:48:d8:75
192.168.103.193 00:0c:6e:55:f7:dd
192.168.103.192 00:30:05:03:8a:08
192.168.103.191 00:11:5b:22:8f:ae
192.168.103.189 00:11:09:6e:b4:22
192.168.103.188 00:e0:4c:e5:06:6c
192.168.103.187 50:78:4c:48:a8:a1
192.168.103.186 00:11:2f:d4:84:6e
192.168.103.185 00:e0:81:25:22:d2
192.168.103.184 00:e0:4c:53:9d:f5
192.168.103.182 00:0f:ea:b2:ab:98
192.168.102.200 00:e0:4c:e4:ff:26
192.168.102.199 00:0f:3d:21:8d:2a
192.168.102.198 00:c0:f6:b0:b4:55
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.196 00:0f:3d:83:1b:2c
192.168.102.195 00:11:11:b4:4b:82
192.168.101.200 00:0e:35:b5:0c:e0
192.168.101.197 00:0a:e6:68:cd:4e
192.168.101.196 00:e0:4c:46:14:2e
192.168.101.195 00:e0:4c:57:bd:61
192.168.101.194 00:0f:1f:b0:95:42
192.168.101.193 00:c0:f6:b0:b4:5c
192.168.101.192 00:11:11:b4:4c:06
192.168.101.102 00:e0:4c:46:14:2e
192.168.103.195 00:11:2f:51:bc:ce
192.168.103.195 192.168.103.194 00:11:2f:51:bc:ce
192.168.103.194 192.168.103.181 00:11:2f:51:bc:ce
192.168.103.181 00:11:2f:51:bc:ce
192.168.102.197 00:0f:3d:21:8d:30
192.168.102.197 00:0f:3d:21:8d:30
192.168.103.195 192.168.103.194 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.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.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.103.199 00:0d:87:4e:4a:ac
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





寂寞烈火
代码:

awk '{gsub(/^ |;/,""};/^lease/{printf $2" "};/^hard/{print $3}' file

结果如下
192.168.104.200 192.168.104.198 192.168.104.197 192.168.104.195 192.168.104.193 192.168.104.192 192.168.104.191 192.168.104.190 192.168.103.183 192.168.103.190 192.168.103.200 192.168.103.199 192.168.103.198 192.168.103.197 192.168.103.196 192.168.103.193 192.168.103.192 192.168.103.191 192.168.103.189 192.168.103.188 192.168.103.187 192.168.103.186 192.168.103.185 192.168.103.184 192.168.103.182 192.168.102.200 192.168.102.199 192.168.102.198 192.168.102.197 192.168.102.196 192.168.102.195 192.168.101.200 192.168.101.197 192.168.101.196 192.168.101.195 192.168.101.194 192.168.101.193 192.168.101.192 192.168.101.102 192.168.103.195 192.168.103.195 192.168.103.194 192.168.103.194 192.168.103.181 192.168.103.181 192.168.102.197 192.168.102.197 192.168.103.195 192.168.103.194 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.103.199 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.103.199 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197





網中人
my try:

$ echo `cat dhcpd.leases` | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/'

结果如下
-bash: $: command not found

修改为
cat dhcpd.leases | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/'
后,结果如下……
(无输出)

论坛徽章:
0
10 [报告]
发表于 2005-04-29 22:18 |显示全部楼层

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

原帖由 "網中人" 发表:

代码:
……
……
……

越来越看不懂了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP