Compare two Paths in a file impossible 
Author Message
 Compare two Paths in a file impossible

Hi,

I want to modify a string by an another in a file.
s/chaine1/chaine2 => OK for the most of string.

But, I have 2 problems:
1) I don't open in R/W my file, only in READ! under Win 2000.
I must use a temporary file to modify my file. But, I must check 2000 files.
If one contains a string A, I want to replace it by a string B.
If I must use a temporary file, I afraid of the answers time.

open(FILE, "+<file") don't modify it.
sysopen(FILE, file, O_RDWR) => it tell me: in READING
open(FILE, ">>file") erase my file.
open(FILE, "+>file") don't modify it.

while (<FILE>)
{
  if (/$string1/)
   {
    $_=~ s/$string1/$string2/g;
    print FILE $_; don't write in a file.
   }

Quote:
}

close (FILE);

2) SECOND PROBLEM without solution to bypass!

if string1=C:\MyDocuments\MyPictures\alpha.gif
string2=D:\MyGIF\compagny.gif
How compare my string1 with a line of the file?
I have tried

if ($_ =~ /$string1/)
if ($_ =~ !$string1!)
I have the error message:
Can't find unicode character property definition via main->r or r.pl
at
unicode/Ls/r.pl line 0.

Help me, please?

Regards,
fafanie



Sun, 03 Oct 2004 15:29:58 GMT  
 Compare two Paths in a file impossible

Quote:

> Hi,

> I want to modify a string by an another in a file.
> s/chaine1/chaine2 => OK for the most of string.

> But, I have 2 problems:
> 1) I don't open in R/W my file, only in READ! under Win 2000.
> I must use a temporary file to modify my file. But, I must check 2000 files.
> If one contains a string A, I want to replace it by a string B.
> If I must use a temporary file, I afraid of the answers time.

There are several possible ways of doing this.  Here is one suggestion. Open
your file for reading, put the contents of the file into an array and print the
array with the string modified appropriately, if it gets modified.  If it
doesn't get modified you don't need to do anything.  If it does get modified,
close the original file handle and open a new one for writing and print the
contents of the array into it.  This may be problematic if your files are huge
and are likely to overload the available memory.  If so, you may need to print a
temporary file as you loop through the file then overwrite your original file
with the temp one if the string has been modified.

Quote:

> 2) SECOND PROBLEM without solution to bypass!

> if string1=C:\MyDocuments\MyPictures\alpha.gif
> string2=D:\MyGIF\compagny.gif
> How compare my string1 with a line of the file?
> I have tried

> if ($_ =~ /$string1/)
> if ($_ =~ !$string1!)
> I have the error message:
> Can't find unicode character property definition via main->r or r.pl
> at
> unicode/Ls/r.pl line 0.

I think  your problem here is that your backslashes are escaping the following
characters.  Either turn them into forward slashes or double them or use single
quotes around them or q( ) as in

$string1=q(C:\MyDocuments\MyPictures\alpha.gif);
$string2=q(D:\MyGIF\compagny.gif);


    open(FILE,"$filename"); # reading it
    $printme=0; # don't rewrite it unless you have to

    while(<FILE>){
        # \Q and \E around the $string1 prevent it from seeing the "\"s
        # as metacharacters
        $printme = 1 if (s/\Q$string1\E/$string2/);

    }
    close FILE;

     if($printme){ # true if string was modified
        open(FILE,">$filename"); # for writing

        close FILE
    }

Quote:
}



Sun, 03 Oct 2004 17:36:10 GMT  
 Compare two Paths in a file impossible

Quote:

> Hi,

> I want to modify a string by an another in a file.
> s/chaine1/chaine2 => OK for the most of string.

> But, I have 2 problems:
> 1) I don't open in R/W my file, only in READ! under Win 2000.
> I must use a temporary file to modify my file. But, I must check 2000 files.
> If one contains a string A, I want to replace it by a string B.
> If I must use a temporary file, I afraid of the answers time.

> open(FILE, "+<file") don't modify it.
> sysopen(FILE, file, O_RDWR) => it tell me: in READING
> open(FILE, ">>file") erase my file.
> open(FILE, "+>file") don't modify it.

> while (<FILE>)
> {
>   if (/$string1/)
>    {
>     $_=~ s/$string1/$string2/g;
>     print FILE $_; don't write in a file.
>    }
> }
> close (FILE);

> 2) SECOND PROBLEM without solution to bypass!

> if string1=C:\MyDocuments\MyPictures\alpha.gif
> string2=D:\MyGIF\compagny.gif
> How compare my string1 with a line of the file?
> I have tried

> if ($_ =~ /$string1/)
> if ($_ =~ !$string1!)
> I have the error message:
> Can't find unicode character property definition via main->r or r.pl
> at
> unicode/Ls/r.pl line 0.

> Help me, please?

> Regards,
> fafanie

in response to your problem 1 .....am not sure if you want to write a
script for that or you are open to implement it from the command
line....

if you are open to implement it from command line, then you can try
something like this(am not sure how well it will work on win2000, but
it seems to work fine on unix variants):

perl -p -i -e 's/chaine1/chaine2/g' *

hth.



Sun, 03 Oct 2004 19:28:22 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. To compare two files and search for a word in both files

2. perl module to compare two files?

3. compare two files

4. compare two files - help!!

5. Help comparing two files using hashes

6. comparing the time stamp of two files

7. How to compare two files?

8. To compare the content in two files..

9. compare two files

10. File I/O: want to compare two or more lines @ once

11. Compare Two text files ?!?!?!

12. Comparing two files

 

 
Powered by phpBB® Forum Software