简体   繁体   English

如何使用TCL正则表达式在文件的特定行中匹配单词

[英]How to match a word in particular line of afile using TCL regexp

Let consider i have one file ip.txt: 假设我有一个文件ip.txt:

IP Address: 192.168.0.100/24 GW: 192.168.0.1 IP地址:192.168.0.100/24 GW:192.168.0.1
IP Address: 192.169.0.100/24 GW: 192.169.0.1 IP地址:192.169.0.100/24 GW:192.169.0.1
IP Address: 192.170.0.100/24 GW: 192.170.0.1 IP地址:192.170.0.100/24 GW:192.170.0.1

The above three lines are content in ip.txt. 以上三行是ip.txt中的内容。 From that file i want to match Gw Ip address on the second line 192.169.0.1 by line basis using TCL regexp. 我想从该文件中使用TCL正则表达式192.169.0.1匹配second line 192.169.0.1上的Gw IP地址。 Please anyone help me to get idea. 请任何人帮助我获得想法。 thanks in advance 提前致谢

Read and discard the first line. 读取并丢弃第一行。 If line 2 matches you are done otherwise terminate. 如果第2行匹配,则完成操作,否则终止。

set f [open $filename r]
if {[gets $f line] == -1} { return -code error "failed to read line 1" }
if {[gets $f line] == -1} { return -code error "failed to read line 2" }
if {![string match "*GW: 192.169.0.1*" $line]} { return -code error "failed to match" }
return

Of course, maybe its not always line 2 and it would be smarted to arrange it to close the file but the above is the simplest version in tcl that will meet the spec provided. 当然,也许它并不总是第2行,并且安排它关闭文件会很聪明,但是以上是tcl中最简单的版本,可以满足所提供的规范。 The opened file gets closed on process exit. 打开的文件在进程退出时关闭。 We don't need a regexp -- string match will do fine. 我们不需要正则表达式-字符串匹配就可以了。 Alternatively: 或者:

set f [open $filename r]
set lineno 1
while {[gets $f line] != -1 && $lineno < 3} {
    if {$lineno == 2 && [regexp {GW: 192.169.0.1} $line]} {
        return 1
    }
    incr lineno
}
close $f
return 0

If I've understood what you're looking for correctly, you want the IP address part (specifically the gateway address) from the second line that matches some pattern? 如果我正确理解了您要寻找的内容,那么您想要第二行的IP地址部分(特别是网关地址)与某种模式匹配吗? The easiest way is probably to parse the whole file in Tcl and then pick the value out of that (because the chances are that if you want the second value, you'll want the third later on). 最简单的方法可能是在Tcl中解析整个文件,然后从中选择值(因为如果您想要第二个值,则稍后需要第三个值)。

proc parseTheFile {filename} {
    set f [open $filename]
    set result {}
    foreach line [split [read $f] "\n"] {
        if {[regexp {IP Address: ([\d.]+)/(\d+) GW: ([\d.]+)} $line -> ip mask gw]} {
            lappend result [list $ip $mask $gw]
        }
    }
    close $f
    return $result
}

set parsed [parseTheFile "the/file.txt"]
set secondGW [lindex $parsed 1 2]
### Alternatively:
# set secondLineInfo [lindex $parsed 1]
# set secondGW [lindex $secondLineInfo 2]
### Or even:
# lassign [lindex $parsed 1] secondIP secondMask secondGW

Like that, you can parse in the file and poke through it at your leisure, even going back and forth in multiple passes, without having to keep the file open or reread it frequently. 这样,您就可以解析文件并在闲暇时浏览它,甚至需要多次往返来回,而不必保持文件打开或频繁重读。 The split \\n / read idiom works well even with a file a few megabytes in size. split \\n / read成语split \\n即使在文件大小为几兆字节的情况下也能很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM