I have an input file in the format
I have written TCL code to extract some values from Other End Path section such that it should be be outputted in the following format
Remember the input text file may have hundreds such path
Expected output
PathID STA spice diff %diff
Path1 816.500 829.700 -13.2 1.603%
However with my current code I am getting below output
PathID STA spice diff %diff
Path1 9.619 12.300 -2.681000000000001 21.796747967479682%
Path2 9.619 12.300 -2.681000000000001 21.796747967479682%
Here is my TCL code, my regex will have to be modified and corrected.
#!/usr/bin/env tclsh
# Open the input file and create an output file set input [open "/mnt/data/inputs.txt" r] set output [open "/mnt/data/results_c.txt" w] puts $output "PathID STA spice diff %diff"
# Initialize variables set current_path "" set capture 0 array set data {}
# Read the file line by line while {[gets $input line] != -1} {
# Check for the start of a new path
if {[regexp {PathID: (d+)} $line match path_id]} {
set current_path $path_id
set capture 0
}
# Detect the "Other End Path" section
if {$current_path ne "" && [string match "*Other End Path:*" $line]} {
set capture 1
continue
}
# Capture the last line of current path's "Other End Path" section
if {$capture == 1 && [regexp {(d+.d+)s+(d+.d+)} $line -> sta spice]} {
set data($current_path) [list $sta $spice]
} }
# Process captured data foreach path [array names data] {
set values $data($path)
set sta [lindex $values 0]
set spice [lindex $values 1]
set diff [expr {$sta - $spice}]
set percent_diff [expr {abs($diff / $spice) * 100}]
puts $output "Path$path $sta $spice $diff $percent_diff%" }
# Close files close $input close $output
puts "Data has been processed and results are written to /mnt/data/results_c.txt"