Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092180
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: Python/Ruby

2010-11-03 11:13:39

原文太多了,懒得复制了,直接看链接:
的6楼。
再添加一个:
password:
====================================
Hostname:     tc-dr-brgch00.tc.abc.com
Server SN:    99AM993
Server Model: System x3650 M3 -[7945O4R]-
IP:           10.23.249.214
CPU Model:    12 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
Memory Size:  65.8783
OS version:   RedHatEnterpriseAS 4 NahantUpdate3
OS kernel:    2.6.9_5-9-0-0
password:
======================================
Hostname:     yf-dr-brgch00.yf01.abc.com
Server SN:    99AM081
Server Model: System x3650 M3 -[7945O4R]-
IP:           10.38.112.31
CPU Model:    12 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
Memory Size:  65.8783
OS version:   RedHatEnterpriseAS 4 NahantUpdate3
OS kernel:    2.6.9_5-9-0-0
============= 代码 =============
 awk 'BEGIN{RS="Hostname";FS="[:\n]"} NR>1{gsub("   "," ");print $2$4$6$8$10$12$14}'

另外补充一个:

Sun Jan 31 00:00:01 2010
        User-Name = "test0023"
        NAS-Port = 17827544
        NAS-IP-Address = 10.60.255.1
        Framed-IP-Address = 10.72.3.127
        NAS-Identifier = "TEST"
        Acct-Status-Type = Stop
        Acct-Delay-Time = 0
        Acct-Input-Octets = 320782848
        Acct-Output-Octets = 1110648668
        Acct-Session-Id = "TEST01100000001752ed887723033"
        Acct-Authentic = RADIUS
        Acct-Session-Time = 26110
        Acct-Input-Packets = 1190968
        Acct-Output-Packets = 1213428
        Acct-Terminate-Cause = User-Request
        Acct-Input-Gigawords = 0
        Acct-Output-Gigawords = 0
        Event-Timestamp = "Jan 31 2010 07:45:43 EAT"
        NAS-Port-Type = Ethernet
        Calling-Station-Id = "00:02:3f:07:48:11"
        NAS-Port-Id = "slot=1;subslot=1;port=0;vlanid=1752;"
        Acct-Tunnel-Packets-Lost = 0
        Service-Type = Framed-User
        Framed-Protocol = PPP
        SK-Attr-60 = 0x31302e37322e332e3132372030303a30323a33663a30373a34383a3131
        SK_Input_Peak_Rate = 1048576
        SK_Input_Average_Rate = 1048576
        SK_Out_Peak_Rate = 10485760
        SK_Out_Average_Rate = 10485760
        SK-Attr-22 = 0x00000000
        SK-Attr-26 = 0x000059f9
        SK-Attr-138 = 0x71696e6764616f6d656469612e636f6d
        Client-IP-Address = 10.60.255.1
        Acct-Unique-Session-Id = "3b3c5639bdf52982"
        Timestamp = 1264867201

Sun Jan 31 00:00:02 2010
        User-Name = "test0013"
        NAS-Port = 17827764
        NAS-IP-Address = 10.60.255.1
        Framed-IP-Address = 10.72.1.131
        NAS-Identifier = "TEST"
        Acct-Status-Type = Start
        Acct-Delay-Time = 0
        Acct-Session-Id = "TEST011000000019720ed8f624654"
        Acct-Authentic = RADIUS
        Event-Timestamp = "Jan 31 2010 07:45:44 EAT"
        NAS-Port-Type = Ethernet
        Calling-Station-Id = "00:10:5c:e8:77:a4"
        NAS-Port-Id = "slot=1;subslot=1;port=0;vlanid=1972;"
        Acct-Tunnel-Packets-Lost = 0
        Service-Type = Framed-User
        Framed-Protocol = PPP
        SK-Attr-60 = 0x31302e37322e312e3133312030303a31303a35633a65383a37373a6134
        SK_Input_Peak_Rate = 1048576
        SK_Input_Average_Rate = 1048576
        SK_Out_Peak_Rate = 10485760
        SK_Out_Average_Rate = 10485760
        SK-Attr-22 = 0x00000000
        SK-Attr-26 = 0x0000604e
        SK-Attr-138 = 0x71696e6764616f6d656469612e636f6d
        Client-IP-Address = 10.60.255.1
        Acct-Unique-Session-Id = "2c32637ece099079"
        Timestamp = 1264867202


获取User-Name,Framed-IP-Address,NAS-Identifier,Acct-Status-Type,Acct-Session-Time,Calling-Station-Id,Acct-Unique-Session-Id,Timestamp的值,如果行中没有Acct-Session-Time则将该值补为0,结果如下:

test0023 10.72.3.127 TEST Stop 26110 00:02:3f:07:48:11 3b3c5639bdf52982 2010-01-30 16:00:01
test0013 10.72.1.131 TEST Start 0 00:10:5c:e8:77:a4 2c32637ece099079 2010-01-30 16:00:02

awk -F'[=\t "]+' 'BEGIN{a["User-Name"]=1;a["Framed-IP-Address"]=2;a["NAS-Identifier"]=3;a["Acct-Status-Type"]=4;a["Acct-Session-Time"]=5;a["Calling-Station-Id"]=6;a["Acct-Unique-Session-Id"]=7}{if(a[$2])b[a[$2]]=$3;
if($2=="Timestamp"){for(n=1;n<=7;n++)printf b[n]==""?0"\t":b[n]"\t";print strftime("%F %T",$3);delete b}}'

if(a[$2]),事先定义了那几个值(1到7),那么awk逐行读取的时候,假如不是关注的行,a[$2]为空,不存取。此时数组b[1-7]对应的值就按照固定的顺序了。
当$2为Timestamp的时候,把之前的逐个打印(可能需要填充0),然后格式化时间戳。最后删掉数组(因为index是几个固定的字段,所以必须要删,否则追加上了)

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