比较两个不相等条目的列表并获取包含匹配元素的行 tcl
compare two lists of unequal entries and get the lines containing matching elements tcl
我有两个格式的文件(这些是列):
color number day
blue 8 Monday
yellow 5 Saturday
green 0 Thursday
orange 6 Tuesday
color number day
yellow 0 Saturday
orange 6 Tuesday
预期输出:
color number day
yellow 0 Saturday different
orange 6 Tuesday same
一段代码:
set color [lindex $file 0]
set number [lindex $file 1]
set day [lindex $file 2] #same assignments for file2 entries
foreach line $file line1 $file2 {
append name "$color $number $day $number_file2 $day_file2 \n"
}
set final [exec column -t << name]
puts $logfile $final
如果在任何一行中发现颜色相同,则必须将整行打印到新文件中的列中。我应该能够将列与数字进行比较。请推荐
下面给出了输出:
set f1 [open file1.txt r]
set f2 [open file2.txt r]
set data1 [read $f1]
set data2 [read $f2]
# Insert the file contents in an array file1
foreach line [split $data1 \n] {
if {[lindex $line 0] eq "color"} {continue}
lassign [lsearch -all -inline -not [split $line] ""] color number day
set file1($color) [list $number $day]
}
# Insert the second file contents in an array file2
foreach line [split $data2 \n] {
if {[lindex $line 0] eq "color"} {continue}
lassign [lsearch -all -inline -not [split $line] ""] color number day
set file2($color) [list $number $day]
}
set out [open output.txt w]
puts $out "color number day"
foreach {color num_day} [array get file2] {
# If the color from file2 exists in file1...
if {[info exists file1($color)]} {
# Compare their numbers...
set comparison [expr {
[lindex $file1($color) 0] == [lindex $file2($color) 0] ? "same" : "different"
}]
# then print in the output file
puts $out "$color $file2($color) $comparison"
}
}
close $out
输出(但不保留行的间距和顺序,但由于具有相同颜色的行可以在文件中的任何位置,我认为这应该不是问题):
color number day
orange 6 Tuesday same
yellow 0 Saturday different
我有两个格式的文件(这些是列):
color number day
blue 8 Monday
yellow 5 Saturday
green 0 Thursday
orange 6 Tuesday
color number day
yellow 0 Saturday
orange 6 Tuesday
预期输出:
color number day
yellow 0 Saturday different
orange 6 Tuesday same
一段代码:
set color [lindex $file 0]
set number [lindex $file 1]
set day [lindex $file 2] #same assignments for file2 entries
foreach line $file line1 $file2 {
append name "$color $number $day $number_file2 $day_file2 \n"
}
set final [exec column -t << name]
puts $logfile $final
如果在任何一行中发现颜色相同,则必须将整行打印到新文件中的列中。我应该能够将列与数字进行比较。请推荐
下面给出了输出:
set f1 [open file1.txt r]
set f2 [open file2.txt r]
set data1 [read $f1]
set data2 [read $f2]
# Insert the file contents in an array file1
foreach line [split $data1 \n] {
if {[lindex $line 0] eq "color"} {continue}
lassign [lsearch -all -inline -not [split $line] ""] color number day
set file1($color) [list $number $day]
}
# Insert the second file contents in an array file2
foreach line [split $data2 \n] {
if {[lindex $line 0] eq "color"} {continue}
lassign [lsearch -all -inline -not [split $line] ""] color number day
set file2($color) [list $number $day]
}
set out [open output.txt w]
puts $out "color number day"
foreach {color num_day} [array get file2] {
# If the color from file2 exists in file1...
if {[info exists file1($color)]} {
# Compare their numbers...
set comparison [expr {
[lindex $file1($color) 0] == [lindex $file2($color) 0] ? "same" : "different"
}]
# then print in the output file
puts $out "$color $file2($color) $comparison"
}
}
close $out
输出(但不保留行的间距和顺序,但由于具有相同颜色的行可以在文件中的任何位置,我认为这应该不是问题):
color number day
orange 6 Tuesday same
yellow 0 Saturday different