如何首先grep一个单词然后如何使用TCL regexp根据该单词grep输出?

时间:2014-08-12 17:58:10

标签: regex tcl expect

这是我的TCL脚本:

set test {

device#more system:/proc/dataplane/fw/application
1 : Amazon Instant Video (num of policy actions: 0)
     port-proto:
     http urls :
              *(www.amazon.com/Instant-Video)*
     dns names :
     https client-hello servNames :
     https server-hello servNames :
     https server-certificate commonNames :
     Application stats :
             Bytes Uploaded : 0
             Bytes Download : 0
             Num Flows : 0
2 : SIP (num of policy actions: 0)
     port-proto:
             Proto 6-6, sport 0-65535, dport 5060-5061
             Proto 17-17, sport 0-65535, dport 5060-5061
     http urls :
     dns names :
     https client-hello servNames :
     https server-hello servNames :
     https server-certificate commonNames :
     Application stats :
             Bytes Uploaded : 0
             Bytes Download : 0
             Num Flows : 0
3 : Photobucket (num of policy actions: 0)
     port-proto:
     http urls :
              *(www.pbsrc.com)*
              *(www.photobucket.com)*
     dns names :
              *.photobucket.co (2)
              *.photobucket.com (2)
     https client-hello servNames :
     https server-hello servNames :
     https server-certificate commonNames :
     Application stats :
             Bytes Uploaded : 34
             Bytes Download : 44
             Num Flows : 78
4 : Filestub (num of policy actions: 0)
     port-proto:
     http urls :
              *(www.filestub.com)*
     dns names :
              *.filestub.com (2)
     https client-hello servNames :
     https server-hello servNames :
     https server-certificate commonNames :
     Application stats :
             Bytes Uploaded : 0
             Bytes Download : 0
             Num Flows : 0
--More--
device#

}



set lines [split $test \n] ; # split using new line char(\n)
set data [join $lines :]
if { [regexp {Photobucket.*(Bytes Uploaded : .* Bytes Download:)} $data x y]} {
        set y [string trimright $y {: }]
        puts "Bytes uploaded : $y"
    }

我正在尝试下载并上传到名为" Photobucket"的应用程序。在$ test变量中。

STEPS that script to do:

1. First identify the word "Photobucket" 
2. Then grep for "Bytes Uploaded : <any number> and Bytes Download : <any number>, Num Flows : <any number> for that application "Photobucket".

Output should be:

Application Name  : "Photobucket" 
Bytes Uploaded : 34
Bytes Download : 44
Num Flows : 78

当我运行我的脚本时,我只得到$ test中的最后一行。

请帮我解决这个问题。

谢谢,

库马尔

2 个答案:

答案 0 :(得分:1)

首先,我认为你没有在你的问题中使用你正在使用的正则表达式,因为你的正则表达式根本没有匹配,因为缺少空间。它应该是:

Photobucket.*(Bytes Uploaded : .* Bytes Download :)

现在,这个正则表达式的问题是.*是贪婪的并且将匹配到字符串的结尾(因为它匹配任何东西和所有东西),然后一次回溯一个字符,直到整个正则表达式是匹配(即最后Bytes Uploaded :Bytes Download :匹配的位置),或者如果未找到匹配项,则正则表达式无法匹配。你需要的是使用.*修饰符使?懒惰(或尽可能少地匹配):

Photobucket.*?(Bytes Uploaded : .*? Bytes Download :)

上述内容将与正确的部分匹配,但y中的值不正确,因为您还有Bytes Uploaded等。装饰不能删除那些。因此,您可能会更改正则表达式:

Photobucket.*?Bytes Uploaded : (\S+):

这会将(\S+)匹配的非空格字符放入变量y。在此之后你不需要修剪。


如果更改正则表达式,则不需要拆分和重新加入:

if { [regexp {Photobucket.*?Bytes Uploaded : (\S+)\s} $test - y]} {
    puts "Bytes uploaded : $y"
}

要获得所有这三个值,您只需要在最后添加它们:

if { [regexp {Photobucket.*?Bytes Uploaded : (\S+)\s+Bytes Download : (\S+)\s+Num Flows : (\S+)\s+} $test - x y z]} {
    puts "Bytes uploaded : $x"
    puts "Byte download : $y"
    puts "Num flows : $z"
}

答案 1 :(得分:1)

您可以使用字符串命令而不是巨型正则表达式

set stats {"Bytes Uploaded" "Bytes Download" "Num Flows"}
set photobucket_idx [string first Photobucket $test]
foreach stat $stats {
    set digits_start [expr {[string first "$stat : " $test $photobucket_idx] + [string length "$stat : "]}]
    set digits_end [expr {[string first \n $test $digits_start] - 1}]
    set digits($stat) [string range $test $digits_start $digits_end]
}
parray digits

输出

digits(Bytes Download) = 44
digits(Bytes Uploaded) = 34
digits(Num Flows)      = 78