This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

compare two files

Help for those learning Tcl or writing their own scripts.
Post Reply
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

compare two files

Post by romain »

hello,

I've 2 files(file1,file2),have the same number of lines,30 lines max.I want to write data which are in the file1 but which is not in the file2.I try use foreach 2 time but I've many difficulties to write these data.Can you help me in the realization of this script?Thx
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

You can take the line from file1 and check if it exists in file2, if not then write it to the file and repeat that on each line.
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

Post by romain »

Yes but,

with

Code: Select all

  foreach a [split [read [open $file1] \n] {
    foreach b [split [read [open $file2] \n] {
     if [string equal $a $b] {
       continue
     } else {
       lappend c $a
     }
    }
   }
Don't works :?:
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

That would probably cause in lappending the same line several times into the list. Try this:

Code: Select all

foreach a [split [read [open $file1] \n] { 
 set f 0
 foreach b [split [read [open $file2] \n] { 
  if {[string equal $a $b]} { 
   # Found it, so we set f to 1 and we break this 2nd loop
   set f 1
   break
  }
 }
 # if f is still 0, then there's no match and we add $a to the list
 if {!$f} { lappend c $a }
}
And don't forget to close the files after finishing from them.
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

Post by romain »

Ok thx i try.

it's not possible to use "lsearch" for verifier the data?

And i have

Code: Select all

file delete -force -- scripts/file1.txt 
file rename -force -- scripts/file2.txt scripts/file1.txt
[17:10] Tcl error [pub:aj]: error deleting "scripts/file1.txt": permission denied

:?:
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Yes you can use lsearch to search through the list, as for deleting check your permissions.
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

Post by romain »

Sir_Fz wrote:Yes you can use lsearch to search through the list, as for deleting check your permissions.
Sorry but how check permisions :oops: :?:

For "lsearch" , Which is for you the fastest way ?

thx
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

romain wrote:
Sir_Fz wrote:Yes you can use lsearch to search through the list, as for deleting check your permissions.
Sorry but how check permisions :oops: :?:

For "lsearch" , Which is for you the fastest way ?

thx
look at file attributes & file owned

tho i would just check there md5 against eachother.. if it doesn't match then i would go and compare.. and do all the rest .. ( if the files arn't big files.. ofcourse..)
XplaiN but think of me as stupid
E
Ehlanna
Voice
Posts: 15
Joined: Thu Jul 21, 2005 12:08 pm

Post by Ehlanna »

Just a very silly thought on this ... how about using the command to shell out and run an OS sommand - is it exec? - and invoking the diff command? Why reinvent the wheel?
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Doing this through [file] is better for the performance and why use [exec] when you can do it through Tcl?
E
Ehlanna
Voice
Posts: 15
Joined: Thu Jul 21, 2005 12:08 pm

Post by Ehlanna »

Oh, I agree, writing it in tcl would be far more fun - just thought I would offer an 'obvious' alternative.
Post Reply