Hi,

Thanks Tanton and Beau for replying.

I wanted to replace the word with another of different length,
but I guess that's not possible.
The reason I wanted to do it this way, was because I thought it
would be faster if I could directly alter the file,

But I doesn't matter now, The method I'm using is also fast,
and it will have to do.

Below is a snippet of what I do...

<snippet>
read FILE, $chrs, $num_of_bytes_till_the_word;
$ln = <FILE>;
$ln =~ s/$word/$anotherword/;
print OUTPUTFILE "$chrs$ln";

read FILE, $chrs, 10000000; ## Read till the end of the file
print OUTPUTFILE "$chrs";
</snippet>

Thanks again.
George P.


On Thu, 5 Dec 2002, Tanton Gibbs wrote:

> perldoc -q "How do I change one line in a file"
> ----- Original Message -----
> From: "George P." <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, December 05, 2002 12:17 AM
> Subject: Substitute a word in a file
>
>
> >
> > Hi,
> >
> > I am looking for a way to open a file in read/write mode,
> > seek to a particular position and then substitute a word
> > with another word.
> >
> > The file is a simple text file.
> >
> > Does anyone know how to do this ?
> >
> > Thanks.
> > George P.
> >
> >


>From [EMAIL PROTECTED] Thu Dec  5 15:39:54 2002
Date: Wed, 4 Dec 2002 19:40:54 -1000
From: Beau E. Cox <[EMAIL PROTECTED]>
To: George P. <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
Subject: RE: Substitute a word in a file

Hi -

Try something like this (untested) pseudocode:

  open (FILE, "+<$filename") or die ...;
# now get to the word you want; you can
# read the file line by line counting bytes read
# or use tell()
  my $pos = 0;
  while (<FILE>) {
# --some test for your word--
# for this example we are looking for 'foo':
    if (/(.*?)foo/) {
      pos += length $1;
      seek (FILE, pos, 0) or die ...
      print FILE 'bar'; # and replacing it with 'bar'
      last; # or you may keep going
      }
    $pos = tell FILE;
    }
close FILE;

Remember, the replacement cannot expand or shrink the file,
it just overwrites in place; I leave the inevitable
syntax and logic errors in the above code as an
exercise for you.

Aloha => Beau.

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of George P.
Sent: Wednesday, December 04, 2002 7:18 PM
To: [EMAIL PROTECTED]
Subject: Substitute a word in a file



Hi,

I am looking for a way to open a file in read/write mode,
seek to a particular position and then substitute a word
with another word.

The file is a simple text file.

Does anyone know how to do this ?

Thanks.
George P.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to