分类: Python/Ruby
2010-11-03 11:13:39
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是几个固定的字段,所以必须要删,否则追加上了)