-   Programming (
-   -   [perl] parse and modify file without creating a copy (

frznchckn 11-04-2010 05:01 PM

[perl] parse and modify file without creating a copy
I don't think this is a "perl one-liner" of find and replace. I'm trying to auto-fill some information in a listing of files. The simplest example is that in the files the following exists:

-- Date :

I would want the script to find this and populate it with something like

-- Date : 20101004-1758

I have a few more similar fields to autofill, and I'd like to do this from within a larger perl script I'm developing to process these files.

So, how I perform in-place file modification from within a perl script?


udaman 11-04-2010 05:24 PM

It fairly easy to do.

Open and read the file into an array.
Search for the -- Date: line pop it into a variable, $pattern.
Rewrite the date line as $line="$pattern $date"
Then rewrite each line of the file out to a new filehandle.

frznchckn 11-04-2010 05:31 PM

Thanks for the response, but I was trying to prevent having to write to a new file.

udaman 11-04-2010 05:38 PM

You can write it out to the same filehandle, if you wish. You still have to read in and write out each line of each file.

All times are GMT -5. The time now is 12:22 AM.