| View previous topic :: View next topic |
| Author |
Message |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Fri Dec 09, 2005 4:58 am Post subject: compare two files |
|
|
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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Fri Dec 09, 2005 7:04 am Post subject: |
|
|
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 |
|
 |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Fri Dec 09, 2005 8:33 am Post subject: |
|
|
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  |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Fri Dec 09, 2005 10:10 am Post subject: |
|
|
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 |
|
 |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Fri Dec 09, 2005 11:19 am Post subject: |
|
|
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
 |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Fri Dec 09, 2005 1:58 pm Post subject: |
|
|
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 |
|
 |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Fri Dec 09, 2005 2:34 pm Post subject: |
|
|
| Sir_Fz wrote: | | Yes you can use lsearch to search through the list, as for deleting check your permissions. |
Sorry but how check permisions
For "lsearch" , Which is for you the fastest way ?
thx |
|
| Back to top |
|
 |
Ofloo Owner
Joined: 13 May 2003 Posts: 953 Location: Belguim
|
Posted: Fri Dec 09, 2005 4:01 pm Post subject: |
|
|
| 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
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 |
|
 |
Ehlanna Voice
Joined: 21 Jul 2005 Posts: 15
|
Posted: Sat Dec 10, 2005 5:37 am Post subject: |
|
|
| 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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sat Dec 10, 2005 12:18 pm Post subject: |
|
|
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 |
|
 |
Ehlanna Voice
Joined: 21 Jul 2005 Posts: 15
|
Posted: Sat Dec 10, 2005 12:48 pm Post subject: |
|
|
| Oh, I agree, writing it in tcl would be far more fun - just thought I would offer an 'obvious' alternative. |
|
| Back to top |
|
 |
|