Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to edit some lines of an existing file in Perl?

Hi,
This is the code I tried to use to open and edit some matching lines of an existing file. But it looks like, it does not work.

Can you please let me know what I am doing wrong?

open my $TEMPFILE, "+<$file" 
                      or die "Couldn't open temp file: $! \n Check if the temp file is created.";	
					  
	while (<$TEMPFILE>){
		s{ \w+ , \s \w+ \s+ \d+ , \s 20\d\d \s+ \d\d : \d\d : \d\d }{Weekday, Month DD, 20YY  HH:MM:SS}xmsg;
		s{ / 20 \d\d \d\d \d\d _  \d\d \d\d }{/20YYMMDD_HHMM}xmsg;
		s{href="(file://///).*?(/20YYMMDD_HHMM)}{$1***$2};	
	}
	
close $TEMPFILE;
}

Open in new window



Thanks,
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Short answer: You can't directly, see: http://perldoc.perl.org/perlopentut.html#Mixing-Reads-and-Writes

Solution: either write each read and / or manipulated line to a temporary file, then truncare and rename the files at the end, alternatively call sed.
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial