TCL_REGEXP ::如何从tcl变量中提取单词并将其放入文本文件中,并用逗号分隔? [英] TCL_REGEXP::How to grep words from tcl variable and put into a text file, seperated with comma?

查看:119
本文介绍了TCL_REGEXP ::如何从tcl变量中提取单词并将其放入文本文件中,并用逗号分隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

set line { 
Jul 24 21:06:40 2014: %AUTH-6-INFO: login[1765]: user 'admin' on 'pts/1' logged
Jul 24 21:05:15 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.58.net. Flow: 0x2
Jul 24 21:04:39 2014: %DATAPLANE-5-: Unrecognized HTTP URL static.58.com. Flow:
Jul 24 21:04:38 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.google-analytics.com. Flow: 0x2265394048.
Jul 24 21:04:36 2014: %DATAPLANE-5-: Unrecognized HTTP URL track.58.co.in. Flow: 0
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.google.co.in. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.google.co.in. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test1. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.google.com. Flow: 0x87073880
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.edgecastcdn.net in dns_hash_table
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.facebook.com. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.fb.com. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:05:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.facebook.com. Flow: 0x87073880
Jul 24 21:05:39 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.facebook.net in dns_hash_table
}

        set urls [list]
        foreach {dummy item} [regexp -all -inline {Server Hello ServerName\s+(\S+)} $line] {
        lappend urls $item
        }
        #puts $res
            set s "*****************************************************"
            set f {}
            set f [open output.txt a]
            if {$f ne {}} {

            foreach url $urls {
            chan puts $f $url

            }
            chan puts $f $s
            chan close $f
            }

如何从上面的变量$ line中复制"URL",客户端Hello ServerName",服务器Hello ServerName",服务器证书CommonName","rname".并将其上传到以逗号分隔的文本文件.

How to grep "URL", "Client Hello ServerName", "Server Hello ServerName", "Server Cert CommonName", "rname" from the above variable $line. and upload this to text file, sepertated by comma.

output.txt的内容应为:

output.txt contents should be :

www.58.net,www.google.co.in,test1.,*.google.com.,cs50.wac.edgecastcdn.net

www.58.net,www.google.co.in,test1.,*.google.com.,cs50.wac.edgecastcdn.net

其中"www.58.net"是使用URL提取的输出. "www.google.co.in"是使用客户端Hello ServerName等grep的输出

where "www.58.net" is the output grepped using URL. "www.google.co.in" is the output grepped using Client Hello ServerName etc

谢谢

Balu P.

推荐答案

您可以捕获URL,然后从结果列表中捕获逗号.一个简单的方法就像...

You can capture the URLs and from the result list then join on commas. A simplistic approach would be like...

set urls [list]
foreach {dummy item} [regexp -all -inline {Server Hello ServerName\s+(\S+)} $line] {
    lappend urls $item
}
set urls [join $urls ,]

尽管网址中可能包含逗号,但您也可以添加引号,也可以在其中转义所有固有的引号...

Though if there can be commas in the urls, you might add quotes and escape any inherent quotes in there too...

set urls [list]
foreach {dummy item} [regexp -all -inline {Server Hello ServerName\s+(\S+)} $line] {
    lappend urls \"[string map {{"} {\"}} $item]\"
}
set urls [join $urls ,]

string map将在此处用反斜杠转义所有引号.

The string map will escape any quotes with a backslash here.

您可以改用制表符代替逗号,以避免出现这些情况:

You might instead use tabs instead of commas to avoid these:

set urls [list]
foreach {dummy item} [regexp -all -inline {Server Hello ServerName\s+(\S+)} $line] {
    lappend urls $item
}
set urls [join $urls \t]


在聊天中,这是完整的代码,其中包含所有其他不同的含义,并使用了 Donal的正则表达式的修改版本:


From chat, here's the full code with all the other different implications and using a modified version of Donal's regexp:

set line { 
Jul 24 21:06:40 2014: %AUTH-6-INFO: login[1765]: user 'admin' on 'pts/1' logged
Jul 24 21:05:15 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.58.net. Flow: 0x2
Jul 24 21:04:39 2014: %DATAPLANE-5-: Unrecognized HTTP URL static.58.com. Flow:
Jul 24 21:04:38 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.google-analytics.
com. Flow: 0x2265394048.
Jul 24 21:04:36 2014: %DATAPLANE-5-: Unrecognized HTTP URL track.58.co.in. Flow: 0
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.google.co.in. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.google.co.in. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test1. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.google.com. Flow: 0x87073880
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.edgecastcdn.net in dns_hash_table
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.facebook.com. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.fb.com. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:05:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.facebook.com. Flow: 0x87073880
Jul 24 21:05:39 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.facebook.net in dns_hash_table
}

set URL [list]
set chs [list]
set shs [list]
set scs [list]
set rname [list]
set cURL 0
set cchs 0
set cshs 0
set cscs 0
set crname 0
foreach {whole type payload} [regexp -all -inline {(?x)
    \y ( URL
      | (?: Client | Server)[ ]Hello[ ]ServerName
      | Server[ ]Cert[ ]CommonName
      | rname\([^)]+\) )
    \s+ ((?:(?![ ]Flow:| in[ ]dns_hash_table).)+)
} $line] {
    switch -regexp $type {
        URL {lappend URL $payload; incr cURL}
        {Client Hello ServerName} {lappend chs $payload; incr cchs}
        {Server Hello ServerName} {lappend shs $payload; incr cshs}
        {Server Cert CommonName} {lappend scs $payload; incr cscs}
        {rname\([^)]+\)} {lappend rname $payload; incr crname}
    }
}

set max [lindex [lsort -decreasing [list $cURL $cchs $cshs $cscs $crname]] 0]
set i 0
set all_list [list]

while {$max != $i} {
    if {[catch {regsub -all {\s} [lindex $URL $i] "" one}]} {set one ""}
    if {[catch {regsub -all {\s} [lindex $chs $i] "" two}]} {set two ""}
    if {[catch {regsub -all {\s} [lindex $shs $i] "" three}]} {set three ""}
    if {[catch {regsub -all {\s} [lindex $scs $i] "" four}]} {set four ""}
    if {[catch {regsub -all {\s} [lindex $rname $i] "" five}]} {set five ""}
    lappend all_list [join [list $one $two $three $four $five] ,]
    incr i
}
puts [join $all_list \n]

ideone演示

这篇关于TCL_REGEXP ::如何从tcl变量中提取单词并将其放入文本文件中,并用逗号分隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆