Mario Jost
Mario Jost

Reputation: 284

compare 2 files with TCL with multiple matches

My question is based on this: How to look for the difference between two large files in tcl? and this: Compare columns between 2 files using TCL entry. I want to compare 2 files with another. I dont have access to TCLLIB. issue with these solutions are, that it does not recognize repeating lines. Example:

File1

interface Vlan1
 description LAN
 shutdown
 ip address 172.16.1.1 255.255.255.0
interface Vlan2
 description LAN
 ip address 172.16.2.1 255.255.255.0
interface Vlan3
 description LAN
 shutdown
 ip address 172.16.3.1 255.255.255.0

File2

interface Vlan1
 description LAN
 ip address 172.16.1.1 255.255.255.0
interface Vlan2
 description LAN
 ip address 172.16.2.1 255.255.255.0
interface Vlan3
 description LAN
 shutdown
 ip address 172.16.3.1 255.255.255.0

So these solutions just go thru line by line and look for example the shutdown entry. So the script does not alert, that there is a difference in configuration, as the line shutdown does indeed exist somewhere in the second file. So how can i correct this? if i could somehow dynamically recognize a 'block' and compare the content with the same block in the second file. but i have no idea how to acomplish this. Can someone maybe steer me into the correct direction?

Upvotes: 0

Views: 115

Answers (1)

glenn jackman
glenn jackman

Reputation: 246754

Here's a bit of a hack: I'm creating a DSL based on those files so that they can be sourced as Tcl code:

proc interface {name} {
    set ::current $name
}

proc description {args} {
    dict set ::interfaces $::current description $args
}

proc shutdown {} {
    dict set ::interfaces $::current shutdown true
}

proc ip {address args} {
    dict set ::interfaces $::current ip $args
}

proc handle {filename} {
    set ::interfaces [dict create]
    source $filename
    return $::interfaces
}

The main hacky-ness is the overuse of global variables.

But we can do:

% set file1_data [handle file1]
Vlan1 {description LAN shutdown true ip {172.16.1.1 255.255.255.0}} Vlan2 {description LAN ip {172.16.2.1 255.255.255.0}} Vlan3 {description LAN shutdown true ip {172.16.3.1 255.255.255.0}}

% set file2_data [handle file2]
Vlan1 {description LAN ip {172.16.1.1 255.255.255.0}} Vlan2 {description LAN ip {172.16.2.1 255.255.255.0}} Vlan3 {description LAN shutdown true ip {172.16.3.1 255.255.255.0}}

And you can compare the two dictionaries in whatever fashion you like.

Upvotes: 2

Related Questions