LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2010, 05:01 PM   #1
frznchckn
Member
 
Registered: Jun 2006
Location: CA
Distribution: Ubuntu, CentOS, Scientific Linux
Posts: 97

Rep: Reputation: 15
Question [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?

Thanks.
 
Old 11-04-2010, 05:24 PM   #2
udaman
Member
 
Registered: Oct 2010
Location: New England, USA
Distribution: OpenSUSE/Slackware64/RHEL/Mythbuntu
Posts: 187

Rep: Reputation: 39
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.
 
Old 11-04-2010, 05:31 PM   #3
frznchckn
Member
 
Registered: Jun 2006
Location: CA
Distribution: Ubuntu, CentOS, Scientific Linux
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks for the response, but I was trying to prevent having to write to a new file.
 
Old 11-04-2010, 05:38 PM   #4
udaman
Member
 
Registered: Oct 2010
Location: New England, USA
Distribution: OpenSUSE/Slackware64/RHEL/Mythbuntu
Posts: 187

Rep: Reputation: 39
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.
 
1 members found this post helpful.
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl read file and parse blocks Goni Programming 34 08-22-2010 09:01 AM
Perl Parse .bin file acandria17 Programming 2 07-07-2009 05:42 AM
perl script to parse this file ohcarol Programming 10 11-02-2006 09:50 AM
optimizing perl parse file. eastsuse Programming 1 12-22-2004 02:49 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM


All times are GMT -5. The time now is 07:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration