Compare columns between 2 files using TCL - tcl

I have 2 files having only one column. Say file1.txt and file2.txt.
Below are the contents inside the file
Inside file1.txt
Tom
Harry
Snowy
Edward
Inside file2.txt
Harry
Tom
Edward
2) I want to write a code that will check each item in the column and print something as below.
"Tom, Harry, Edward" are present in both the files
Snowy is there in file1.txt but not in file2.txt
3) Basic code
set a [open file1.txt r]
set b [open file2.txt r]
while {[gets $a line1] >= 0 && [gets $b line2] >= 0} {
foreach a_line $line1 {
foreach b_line $line2 {
if {$a_line == $b_line } {
puts "$a_line in file test1 is present in $b_line in file test2\n"
} else {
puts "$a_line is not there\n"
}
}
}
}
close $a
close $b
Issue is that it is not checking each name in the column.
Any suggestions.
Thanks in advance.
Neel

What you want to do is read each file separately and not have nested loops:
# read the contents of file1 into an associative array
# store the user as an array **key** for fast lookoup
set fh [open "file1.txt" r]
while {[gets $fh user] != -1} {
set f1tmp($user) ""
}
close $fh
# read file2 and compare against file1
array set users {both {} file1 {} file2 {}}
set fh [open "file2.txt" r]
while {[gets $fh user] != -1} {
if {[info exists f1tmp($user)]} {
lappend users(both) $user
unset f1tmp($user)
} else {
lappend users(file2) $user
}
}
close $fh
set users(file1) [array names f1tmp]
parray users
users(both) = Harry Tom Edward
users(file1) = Snowy
users(file2) =
Or as Donal suggests, use tcllib
package require struct::set
set fh [open file1.txt r]
set f1users [split [read -nonewline $fh] \n]
close $fh
set fh [open file2.txt r]
set f2users [split [read -nonewline $fh] \n]
close $fh
set results [struct::set intersect3 $f1users $f2users]
puts "in both: [join [lindex $results 0] ,]"
puts "f1 only: [join [lindex $results 1] ,]"
puts "f2 only: [join [lindex $results 2] ,]"
in both: Harry,Tom,Edward
f1 only: Snowy
f2 only:

Related

how to compare lines of two files and change the matched line in one file in tcl

tcl
I wanna compare two files line by line.
file1
abc
123
a1b2c3
file2
abc
00 a1b2c3
if the line of file1 matched one of the line of file2, change the line of file1 to the line of file2
so the output file woule be like that.
file1
abc
123
00 a1b1c3
please help me
thank you
Here's a working example, if necessary adjust file paths to fit your needs.
This code makes a temporary work file that overwrites the original file1 at end.
set file1Fp [open file1 "r"]
set file1Data [read $file1Fp]
close $file1Fp
set file2Fp [open file2 "r"]
set file2Data [read $file2Fp]
close $file2Fp
set tempFp [open tempfile "w"]
foreach lineFile1 [split $file1Data "\n"] {
set foundFlag 0
foreach lineFile2 [split $file2Data "\n"] {
if { $lineFile1 == {} } continue
if { [string match "*$lineFile1*" $lineFile2] } {
set foundFlag 1
puts $tempFp "$lineFile2"
}
}
if { $foundFlag == 0 } {
puts $tempFp "$lineFile1"
}
}
close $tempFp
file rename -force tempfile file1
You could write
set fh [open file2]
set f2_lines [split [read -nonewline $fh] \n]
close $fh
set out_fh [file tempfile tmp]
set fh [open file1]
while {[gets $fh line] != -1} {
foreach f2_line $f2_lines {
if {[regexp $line $f2_line]} {
set line $f2_line
break
}
}
puts $out_fh $line
}
close $fh
close $out_fh
file rename -force $tmp file1
Depending on how you want to compare the two lines, the regexp command can also be expressed as
if {[string match "*$line*" $f2_line]}
if {[string first $line $f2_line] != -1}

tcl how to read files and show the certain words

I have a question few days ago ,but I think my expression is not clear and I separate my question into many small questions.
I have many files of process and it contain versions, I have regexp certain line of them and import them into a txt file , the txt format is like
#process #AA_version #BB_version
a11 Aa/10.10-d87_1 Bb/10.57-d21_1
a15 Aa/10.15-d37_1 Bb/10.57-d28_1
a23 Aa/10.20-d51_1 Bb/10.57-d29_3
and each process correspond its AA_version and BB_version
I want to write a tcl named get_tool_version.tcl to show /modify(not replace) the content
If I tclsh get_tool_version.tcl and input process and it will read the txt file and show it's
AA_version=Aa/
BB_version=Bb/
and then I can modify the string of AA and BB version
there is my code
set fp [open tool_version r+]
set file_data [read $fp]
close $fp
set data [split $file_data "\n"]
#input the process
set name [gets stdin] ->#and it'll show correspond AAand BB version
but I don't know how to show it's AA_version and BB_version
and how to modify them.
Or I need to use array?
thanks
Here's a way:
set fh [open tool_version r]
set data [dict create]
while {[gets $fh line] != -1} {
regexp {(\w+)\s+Aa/(\S+)\s+Bb/(\S+)} $line -> process aa bb
dict set data $process Aa $aa
dict set data $process Bb $bb
}
close $fh
set name a15 ;# you would get input from user here
puts "process = $name; Aa = [dict get $data $name Aa]; Bb = [dict get $data $name Bb]"
process = a15; Aa = 10.15-d37_1; Bb = 10.57-d28_1
The Tcl regex syntax is here: https://www.tcl-lang.org/man/tcl8.6/TclCmd/re_syntax.htm
here's my final version
set fp [open tool_version r]
set process [gets stdin]
while {[gets $fh line] != -1} {
if (regexp $process $line) {
dict set process1 Aa: [lindex $line 1]
dict set process1 Bb: [lindex $line 2]
puts "Aa: [lindex $line 1]"
puts "Bb: [lindex $line 2]"
}
}
close $fp
Thanks~

how to turn an array into a list

I'm trying to read each line in a file but it give an error every time I run it:
set fr [open temp.txt r]
set a [read $fr]
set b [split $a '\n']
foreach i $b{
*code*
}
This code works fine:
set fr [open input_file.txt r]
set a [read $fr]
close $fr
set b [split $a \n]
set fa [open temp.txt a]
foreach i $b {
#Process items in list b
puts $fa $i
}
close $fa

Tcl echo file contents to transcript

i'm using some simulator that uses Tcl for transcript commands (Questa sim)
i want to echo file content like "cat" command in unix.
can it be done in one line command at tcl? is it possible to "cat" just the 5 first lines of file
In one line
puts [read [open data.dat r]]
OR step by step..
set handle [open data.dat r]
puts [read $handle]
close $handle
To open a file and echo its contents to standard output (just like cat), do this:
set f [open $filename]
fcopy $f stdout
close $f
To just do the first five lines (which is just like head -5), use this procedure:
proc head {filename {lineCount 5}} {
set f [open $filename]
for {set i 0} {$i < $lineCount} {incr i} {
if {[gets $f line] >= 0} {
puts $line
}
}
close $f
}
It takes more work because it's more complex to detect line endings than it is to just ship bytes around.
Here is the following code, to read 5 lines at a time from a given file.
#!/usr/bin/tclsh
set prev_count -1
set fp [open "input-file.txt" "r"]
set num_lines [split [read $fp] \n]
for {set i 4} {$i < [llength $num_lines]} { incr i 5} {
set line_5 [lrange $num_lines [incr prev_count] $i ]
set prev_count $i
puts "$line_5\n\n"
}

How to get the data between two strings from a file in tcl?

In TCL Scripting:
I have a file in that i know how to search a string but how to get the line number when string is found.please answer me if it is possible
or
set fd [open test.txt r]
while {![eof $fd]} {
set buffer [read $fd]
}
set lines [split $buffer "\n"]
if {[regexp "S1 Application Protocol" $lines]} {
puts "string found"
} else {puts "not found"}
#puts $lines
#set i 0
#while {[regexp -start 0 "S1 Application Protocol" $line``s]==0} {incr i
#puts $i
#}
#puts [llength $lines]
#puts [lsearch -exact $buffer S1]
#puts [lrange $lines 261 320]
in the above program i am getting the output as string found .if i will give the string other than in this file i am getting string not found.
The concept of 'a line' is just a convention that we layer on top of the stream of data that we get from a file. So if you want to work with line numbers then you have to calculate them yourself. The gets command documnetion contains the following example:
set chan [open "some.file.txt"]
set lineNumber 0
while {[gets $chan line] >= 0} {
puts "[incr lineNumber]: $line"
}
close $chan
So you just need to replace the puts statement with your code to find the pattern of text you want to find and when you find it the value of $line gives you the line number.
To copy text that lies between two other lines I'd use something like the following
set chan [open "some.file.txt"]
set out [open "output.file.txt" "w"]
set lineNumber 0
# Read until we find the start pattern
while {[gets $chan line] >= 0} {
incr lineNumber
if { [string match "startpattern" $line]} {
# Now read until we find the stop pattern
while {[gets $chan line] >= 0} {
incr lineNumber
if { [string match "stoppattern" $line] } {
close $out
break
} else {
puts $out $line
}
}
}
}
close $chan
The easiest way is to use the fileutil::grep command:
package require fileutil
# Search for ipsum from test.txt
foreach match [fileutil::grep "ipsum" test.txt] {
# Each match is file:line:text
set match [split $match ":"]
set lineNumber [lindex $match 1]
set lineText [lindex $match 2]
# do something with lineNumber and lineText
puts "$lineNumber - $lineText"
}
Update
I realized that if the line contains colon, then lineText is truncated at the third colon. So, instead of:
set lineText [lindex $match 2]
we need:
set lineText [join [lrange $match 2 end] ":"]