TCL_REGEXP ::如何使用TCL正则expression式从variables中search5个不同的单词。 如何发送输出到Excel表格的每一列?
我的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 item [regexp -all -inline {URL\s+\S+} $line] { lappend urls [lindex $item 1] } #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 }
我的要求:
REQ 1.我需要从$ linevariablesgrep下面的东西。
-
urlwww.58.net
-
客户端Hello ServerName www.google.co.in。
-
服务器Hello ServerName test1
-
服务器证书CommonName * .google.com。
-
rname(TYPE_A)cs50.wac.edgecastcdn.net
URL,Client Hello ServerName,Server Hello ServerName,Server Cert CommonName,rname是常用字段。 如上图所示,无论出现什么,都需要grep。
REQ 2.当我浏览一个URL时,Iam得到$ line的内容。 当我打开一个URL时,我的脚本应该自动grep上面的东西,并存储在MS Excel文件中。应该有Excel列表中的每个领域的5列。 当在$行中find“URL”时,它应该放在Excel表格的第1列。 当find“Client Hello ServerName”时,应将其移至Excel表的第2列。 像这样,我想把所有5个数据上传到excel表格。
使用上面提供的脚本,我可以grep的URL和能够上传到.txt文件。
请指导我你的想法。 非常感谢。
谢谢,
巴卢
像大多数可再生能源发动机一样,Tcl允许通过使用|
来替代 运营商。 这可以让你做到:
# This is using expanded syntax foreach {whole type payload} [regexp -all -inline {(?x) \y ( URL | (?: Client | Server)[ ]Hello[ ]ServerName | Server[ ]Cert[ ]CommonName | rname\(TYPE_A\) ) \s+ (\S+) } $line] { puts "type = $type" puts "payload = [string trimright $payload .]" }
(棘手的位: \y
表示“单词边界”,否则实际空间必须写为[ ]
因为扩展模式吞噬空白。
当我尝试你的数据,我得到这个输出(每匹配input线两条输出线):
type = URL 有效载荷= www.58.net type = URL 有效载荷= static.58.com type = URL 有效负载= www.google- analytics type = URL 有效载荷= track.58.co.in type = URL 有效载荷= www.google.co.in 键入= Client Hello ServerName 有效载荷= www.google.co.in 键入= Server Hello ServerName payload = test1 键入= Server Cert CommonName payload = * .google.com type = rname(TYPE_A) 有效载荷= cs50.wac.edgecastcdn.net type = URL 有效载荷= www.facebook.com 键入= Client Hello ServerName 有效载荷= www.fb.com 键入= Server Hello ServerName 有效载荷=testing 键入= Server Cert CommonName 有效载荷= * .facebook.com type = rname(TYPE_A) 有效载荷= cs50.wac.facebook.net
我不知道这是不是你想要的,但是非常接近。
对于第二个问题,您需要生成一个CSV文件(Tcl在社区库tcllib中获得了一个包),或者使用COM与Excel交谈并直接操作其中的内容(Tcom包是通常推荐的方法那里)。 哪一个最好取决于你没有告诉我们的因素; 你应该在解释情况是什么时作为一个单独的问题来询问(例如,是否存在一个现有的电子表格,或者电子表格是否将被重新创build)。
您可以编写一个过程,并将types作为文件名和有效内容传递为要写入的数据。 我在下面写了一个。
proc type2file {filename payload} { set name [string trim $filename].txt set fp [open $name a] puts $fp $payload close $fp }
在里面调用这个循环。 请让我知道,如果它适合你。