Coding style 清理TCL列表和;输出

Coding style 清理TCL列表和;输出,coding-style,tcl,code-cleanup,Coding Style,Tcl,Code Cleanup,您好,我正在编写一个小脚本,它允许我解析输入中的信息。这是可行的,但我知道有更好的方法可以做到这一点。求你了,我正在努力学习[自学],所以把我撕碎吧。投入如下: C7-0-4-U1 36.5 mHz IN7-0-4-0 567 mHz 00:15:d0:e3:b0:41 online(pt) 10.106.156.129 42.0 -0.5 35.1 -3.0 38.7 0.0E+000 0.0E+000 9 0 12:2

您好,我正在编写一个小脚本,它允许我解析输入中的信息。这是可行的,但我知道有更好的方法可以做到这一点。求你了,我正在努力学习[自学],所以把我撕碎吧。投入如下:

C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 00:15:d0:e3:b0:41   online(pt)  10.106.156.129  42.0    -0.5    35.1    -3.0    38.7    0.0E+000    0.0E+000    9   0    12:20:32 AM    rZ5 1       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 2c:9e:5f:de:ed:36   w-online(pt)    10.113.52.11    36.5    0.0 35.1    -5.0    37.7    4.9E-006    0.0E+000    9   0    12:20:32 AM    r4G 0       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz e4:83:99:6d:57:ad   w-online(pt)    10.113.45.239   43.5    0.0 35.1    -4.6    39.5    5.8E-006    0.0E+000    8   0    12:20:34 AM    r4G 0       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 3c:75:4a:9c:7b:92   w-online(pt)    10.109.238.61   42.2    -0.5    33.9    -14.4   34.6    4.9E-006    0.0E+000    199 4    12:20:33 AM    rC2 0       
#GET INPUT FROM CLIPBOARD
set Input [sh_set clipboard]
#REMOVE ALL EXCESSIVE WHITESPACE
regsub -all {\s{3,}} $Input "\n" CleanInput
#SET THE INPUT AS LIST
set List [split $CleanInput "\n"]
#GET LIST ITEMS
set Cust1 [lindex $List 1]
set Cust2 [lindex $List 2]
set Cust3 [lindex $List 3]
set Cust4 [lindex $List 4]
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust1 C1MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust1 C1IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust2 C2MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust2 C2IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust3 C3MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust3 C3IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust4 C4MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust4 C4IpAddress
return "$C1MacAddress\t$C1IpAddress\n$C2MacAddress\t$C2IpAddress\n$C3MacAddress\t$C3IpAddress\n$C4MacAddress\t$C4IpAddress"
所需的输出如下:

00:15:D0:E3:B0:4110.106.156.129
2C:9E:5F:DE:ED:3610.113.52.11
E4:83:99:6D:57:AD10.113.45.239
3C:75:4A:9C:7B:9210.109.238.61

我拥有的代码如下:

C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 00:15:d0:e3:b0:41   online(pt)  10.106.156.129  42.0    -0.5    35.1    -3.0    38.7    0.0E+000    0.0E+000    9   0    12:20:32 AM    rZ5 1       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 2c:9e:5f:de:ed:36   w-online(pt)    10.113.52.11    36.5    0.0 35.1    -5.0    37.7    4.9E-006    0.0E+000    9   0    12:20:32 AM    r4G 0       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz e4:83:99:6d:57:ad   w-online(pt)    10.113.45.239   43.5    0.0 35.1    -4.6    39.5    5.8E-006    0.0E+000    8   0    12:20:34 AM    r4G 0       


C7-0-4-U1   36.5 mHz    IN7-0-4-0   567 mHz 3c:75:4a:9c:7b:92   w-online(pt)    10.109.238.61   42.2    -0.5    33.9    -14.4   34.6    4.9E-006    0.0E+000    199 4    12:20:33 AM    rC2 0       
#GET INPUT FROM CLIPBOARD
set Input [sh_set clipboard]
#REMOVE ALL EXCESSIVE WHITESPACE
regsub -all {\s{3,}} $Input "\n" CleanInput
#SET THE INPUT AS LIST
set List [split $CleanInput "\n"]
#GET LIST ITEMS
set Cust1 [lindex $List 1]
set Cust2 [lindex $List 2]
set Cust3 [lindex $List 3]
set Cust4 [lindex $List 4]
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust1 C1MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust1 C1IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust2 C2MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust2 C2IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust3 C3MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust3 C3IpAddress
regexp -all {(?:[[:xdigit:]]{2}([.-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}} $Cust4 C4MacAddress
regexp -all {10\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)} $Cust4 C4IpAddress
return "$C1MacAddress\t$C1IpAddress\n$C2MacAddress\t$C2IpAddress\n$C3MacAddress\t$C3IpAddress\n$C4MacAddress\t$C4IpAddress"
以下是我的解决方案:

# read entire file
set fid [open file.txt r]
set txt [read $fid]
close $fid

# split into lines
set lines0 [split $txt \n]

# take only non-empty lines
set lines {}
foreach line $lines0 {
    if {[string trim $line] ne ""} {
        lappend lines $line
    }
}

# extract the required data from each line
foreach line $lines {
    set data [regexp -inline {(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})\s+\S+\s+ (\d+.\d+.\d+.\d+)} $line]
    foreach {dummy x y} $data {
        puts "$x\t$y"
    }
}
我使用的regexp搜索由冒号分隔的六个2字符单词组成的模式,然后是一个空格,后跟一个单词,后跟一个空格(\s+\s+\s+)然后是ip的模式 地址

regexp返回一个三元组列表(是平面的,不是列表列表),其中每个三元组包括整个匹配,然后是第一个parnthesis匹配,然后是scond匹配。因此,虚拟变量

或者,您也可以使用regexp,因为每行的字数相同。由于在tcl中,您可以将字符串视为列表,其项目由空格分隔,因此您可以通过以下方式更简单地提取数据:

foreach line $lines {
    set x [lindex $line 6]
    set y [lindex $line 8]
    puts "$x\t$y"
}

好吧,假设我们从一个txt文件加载它,我会这样做:

set input [open "file.txt" r]
set output [open "output.txt" w]
set count 1

while {[gets $input line] != -1} {
    if {$line == ""} {continue} #Skip empty lines
    regexp -all {(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})} $line - MacAddress #trap the mac address and store into $MacAddress
    regexp -all {(\d+\.\d+\.\d+\.\d+)} $line - IpAddress #trap the IP address and store into $IpAddress
    puts $output "Customer $count: $MacAddress $IpAddress"
    incr count
}

close $input
close $output
我只是觉得如果你能说哪个客户是哪个^^会很有趣。如果不需要额外功能,您可以删除部件“Customer$count”和任何涉及“$count”的行

或者,如果输入数据是表格形式(意味着您需要的信息在相同的列中,我会选择:

set input [open "file.txt" r]
set output [open "output.txt" w]
set count 1

while {[gets $input line] != -1} {
    if {$line == ""} {continue} #Skip empty lines
    regsub -all { +} $line " " newline #trims excess spaces
    set MacAddress [lindex [split $line " "] 6]
    set MIpAddress [lindex [split $line " "] 8]
    puts $output "Customer $count: $MacAddress $IpAddress"
    incr count
}

close $input
close $output

问题是什么?awk在这方面会有所帮助,TCL似乎是一种过度使用。实际上,这只是为了变得更好,我知道有一个“foreach”可以用比我少得多的行数来完成它。我通常可以想出如何使某些东西工作……我现在正在努力简化我的代码,使其高效工作。在TCL中这样做的原因是我使用一个程序来运行它。我使用PcShorthand 10来自动化许多重复的工作。这个程序对于那些打字频繁的人来说真的很好。谢谢你们的回复。正如我所说的,我正在学习TCL,并且知道如何改进我的工作。