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]




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

Reply via email to