egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

compare two files

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
romain
Voice


Joined: 16 Oct 2005
Posts: 12

PostPosted: Fri Dec 09, 2005 4:58 am    Post subject: compare two files Reply with quote

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
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Fri Dec 09, 2005 7:04 am    Post subject: Reply with quote

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.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
romain
Voice


Joined: 16 Oct 2005
Posts: 12

PostPosted: Fri Dec 09, 2005 8:33 am    Post subject: Reply with quote

Yes but,

with


Code:
  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 Question
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Fri Dec 09, 2005 10:10 am    Post subject: Reply with quote

That would probably cause in lappending the same line several times into the list. Try this:
Code:
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.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
romain
Voice


Joined: 16 Oct 2005
Posts: 12

PostPosted: Fri Dec 09, 2005 11:19 am    Post subject: Reply with quote

Ok thx i try.

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

And i have
Code:
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

Question
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Fri Dec 09, 2005 1:58 pm    Post subject: Reply with quote

Yes you can use lsearch to search through the list, as for deleting check your permissions.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
romain
Voice


Joined: 16 Oct 2005
Posts: 12

PostPosted: Fri Dec 09, 2005 2:34 pm    Post subject: Reply with quote

Sir_Fz wrote:
Yes you can use lsearch to search through the list, as for deleting check your permissions.


Sorry but how check permisions Embarassed Question

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

thx
Back to top
View user's profile Send private message
Ofloo
Owner


Joined: 13 May 2003
Posts: 953
Location: Belguim

PostPosted: Fri Dec 09, 2005 4:01 pm    Post subject: Reply with quote

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 Embarassed Question

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
Back to top
View user's profile Send private message Visit poster's website
Ehlanna
Voice


Joined: 21 Jul 2005
Posts: 15

PostPosted: Sat Dec 10, 2005 5:37 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sat Dec 10, 2005 12:18 pm    Post subject: Reply with quote

Doing this through [file] is better for the performance and why use [exec] when you can do it through Tcl?
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
Ehlanna
Voice


Joined: 21 Jul 2005
Posts: 15

PostPosted: Sat Dec 10, 2005 12:48 pm    Post subject: Reply with quote

Oh, I agree, writing it in tcl would be far more fun - just thought I would offer an 'obvious' alternative.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber