TCL_REGEXP ::如何使用TCL regexp从变量中重复5个不同的单词.以及如何将贪婪的输出发送到Excel工作表的每一列? [英] TCL_REGEXP::How to grep 5 diifferent words from a variable using TCL regexp. And how to send greeped output to each column of excel sheet?

查看:90
本文介绍了TCL_REGEXP ::如何使用TCL regexp从变量中重复5个不同的单词.以及如何将贪婪的输出发送到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.我需要从$ line变量中获取以下内容.

REQ 1. I need to grep the following things from $line variable.

  1. URL www.58.net

  1. URL www.58.net

客户端Hello ServerName www.google.co.in.

Client Hello ServerName www.google.co.in.

服务器Hello ServerName test1

Server Hello ServerName test1

服务器证书公用名* .google.com.

Server Cert CommonName *.google.com.

rname(TYPE_A)cs50.wac.edgecastcdn.net

rname(TYPE_A) cs50.wac.edgecastcdn.net

URL,客户端Hello ServerName,服务器Hello ServerName,服务器证书CommonName,rname是公共字段.需要将上面显示的所有单词改写为上面的单词.

URL, Client Hello ServerName, Server Hello ServerName, Server Cert CommonName, rname are the common fields. Need to grep the words whatever appearing after that as shown above.

REQ 2.浏览URL时,Iam获取$ line的内容.当我打开一个URL时,我的脚本应该自动grep以上内容,并将其存储在MS Excel文件中.excel工作表中应有5列,每个字段对应一个字段.当在$ line中找到"URL"时,它应该进入Excel工作表的第1列.找到客户端Hello ServerName"后,应将其移到excel工作表的第2列.像这样,我想将所有5个数据上传到Excel工作表.

REQ 2. When I browse a URL, Iam getting the contents of $line. When I open a URL, my script should automatically grep the above things, and store in MS Excel file.There should be 5 columns in excel sheet each for one field. When "URL" found in a $line, it should Go and sit into column 1 of excel sheet. When "Client Hello ServerName" found, it should be moved to column 2 of excel sheet. Like this I want to upload all 5 datas to excel sheet.

使用上面提供的脚本,我可以grep URL并可以上传到.txt文件.

Using my script provided above, I am able to grep URL's and able to upload into an .txt file.

请指导我您的想法.提前非常感谢.

Please guide me your ideas. Thanks a lot in advance.

谢谢

Balu P.

推荐答案

与大多数RE引擎一样,Tcl通过使用|运算符允许替代.这可以让您执行以下操作:

Like most RE engines, Tcl's allows alternation through the use of the | operator. This lets you do:

# 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表示单词边界",由于扩展模式会吞下空白,因此必须将实空间写为[ ].)

(The tricky bits: \y means "word boundary", and real spaces have to be written as [ ] because of expanded mode swallowing whitespace otherwise.)

当我尝试使用您的数据时,我得到以下输出(每条匹配的输入线有两条输出线):

When I try with your data, I get this output (two output lines per matched input line):


type = URL
payload = www.58.net
type = URL
payload = static.58.com
type = URL
payload = www.google-analytics
type = URL
payload = track.58.co.in
type = URL
payload = www.google.co.in
type = Client Hello ServerName
payload = www.google.co.in
type = Server Hello ServerName
payload = test1
type = Server Cert CommonName
payload = *.google.com
type = rname(TYPE_A)
payload = cs50.wac.edgecastcdn.net
type = URL
payload = www.facebook.com
type = Client Hello ServerName
payload = www.fb.com
type = Server Hello ServerName
payload = test
type = Server Cert CommonName
payload = *.facebook.com
type = rname(TYPE_A)
payload = cs50.wac.facebook.net

我不知道这是否正是您想要的,但它非常接近.

I don't know if this is exactly what you want, but it's very close.

对于第二个问题,您需要生成一个CSV文件(Tcl在社区库tcllib中提供了该文件),或者需要使用COM与Excel对话并直接在其中进行操作(Tcom包是一般推荐的方法).哪个最好,取决于您没有告诉我们的因素;您应该在解释情况时作为一个单独的问题问(例如,是否存在现有电子表格,或者将在 de novo 中创建电子表格.)

For the second question, you need to either generate a CSV file (Tcl's got a package for that in the community library, tcllib) or to use COM to talk to Excel and manipulate things in there directly (the Tcom package is the generally recommended approach there). Which is best will depend on factors that you are not telling us; you should ask that as a separate question while explaining what the situation is (e.g., is there an existing spreadsheet or will the spreadsheet be created de novo.)

这篇关于TCL_REGEXP ::如何使用TCL regexp从变量中重复5个不同的单词.以及如何将贪婪的输出发送到Excel工作表的每一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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