CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Read and edit text file

 



diddyp20
New User

Oct 5, 2012, 1:07 PM

Post #1 of 2 (1091 views)
Read and edit text file Can't Post

Hello,

I am a newbie on Perl and wanted to know what is the syntax to read an input file, edit it and save the result in an output file.

for instance I have the a text file have the data:

Server Date Time State

QA01 10/5/2012 4:04 idle

I want to read the file and get a result looking like this

Server Date Time State Year Month Day

QA01 10/5/2012 4:04 idle 2012 10 5

Is it possible? if yes how can I do it?



Thank you


Laurent_R
Veteran / Moderator

Oct 6, 2012, 1:00 AM

Post #2 of 2 (1082 views)
Re: [diddyp20] Read and edit text file [In reply to] Can't Post

Hi,

sure it is possible.

You will need to open the input file in read mode and read its contents line by line, open a new file in write mode.

One of the possible way to edit the line is as follows:


Code
while (my $line = <$DATA_IN>) { 
if ($line =~ m[^\w{4} (\d\d?)/(\d\d?)/(\d{4}) \d\d?:\d\d?]) {
chomp $line; # removes trailing carriage return
my $new_date = "$3 $1 $2"; # new order of date components
$line = "$line $new_date \n"; # add new date format to existing line
}
print $DATA_OUT $line;
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives