Chinaunix首页 | 论坛 | 博客
  • 博客访问: 907871
  • 博文数量: 75
  • 博客积分: 1216
  • 博客等级: 少尉
  • 技术积分: 1998
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-11 16:20
个人简介

优秀是一种习惯

文章分类

全部博文(75)

文章存档

2014年(1)

2013年(29)

2012年(45)

分类: Python/Ruby

2012-12-18 14:59:48

[root@station1 ~]# cat data
#[ERROR] [fxmon] [path] attr {vscsi_err_recov} should be , current is
#[ERROR] [fxmon] [path] attr {vscsi_err_recov} should be [fast_fail], current is
#[ERROR] [fxmon] [path] [backing-device] <${_bd}> is an unknow type disk, please check"
#[ERROR] [dwn2] [path] attr {vscsi_err_recov} should be , current is
#[ERROR] [dwn2] [path] attr {vscsi_path_to} should be <30>, current is <0>
#[ERROR] [dwn2] [disk] attr {hcheck_interval} should be <60>, current is <0>

这是文本处理结果:
fxmon|error|path|vscsi0| attr {vscsi_err_recov} should be , current is
fxmon|error|path|vscsi0| attr {vscsi_err_recov} should be [fast_fail], current is
fxmon|error|path|vscsi0| [backing-device] <${_bd}> is an unknow type disk, please check"

这个问题是用sed比是用awk要简单的多,因为其中很多的[],而且不止前三个字段中包含[],后面的数据也有。所以想到是用sed去处理,代码如下:

 sed -e 's/^#\[\([^[]*\)\] \[\([^[]*\)\] \[\([^[]*\)\] <\(.\{,10\}\)>/\2|\L\1\E|\3|\4|/' data

perl的代码如下
[root@station1 ~]# cat data | perl -ne 's/^#\[(.*)\] \[(.*)\] \[(.*)\] <(.*?[0-9])>/\2|\L\1\E|\3|\4|/;print '
fxmon|error|path|vscsi0| attr {vscsi_err_recov} should be , current is
fxmon|error|path|vscsi0| attr {vscsi_err_recov} should be [fast_fail], current is
fxmon|error|path|vscsi0| [backing-device] <${_bd}> is an unknow type disk, please check"
dwn2|error|path|vscsi1| attr {vscsi_err_recov} should be , current is
dwn2|error|path|vscsi1| attr {vscsi_path_to} should be <30>, current is <0>
dwn2|error|disk|hdisk0| attr {hcheck_interval} should be <60>, current is <0>


阅读(1485) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~