Reputation: 186
I need to search for tag having value h:mm:ss and replace this value with h hours, mm minutes, and ss seconds to my outputfile.
I am struggling with the replace syntax. write now I am having an input file to read on.
here is what i am trying -
open(my $fh, '<:encoding(UTF-8)', $filein)
or die "Could not open file '$filein' $!";
while(<$fh>) {
chomp $_;
while ( $_ =~ /<time>\s*([^<]*)\s*<\/time>/g ) {
chomp $1;
push(@time, $1);
}
}
close($fh);
open(my $wr, '<:encoding(UTF-8)', $filein)
or die "Could not open file '$filein' $!";
open(FILE, ">$fileout") or die "File not found";
print FILE "average time = 1:00:43\n";
while (my $line = <>){
$line =~ m!/s/[%d:%02d:%02d]/g/hacker/!; #hacker is my replaced value for testing purpose.
#$x =~ s/cat/hacker/;
#s/<PREF>/ABCD/g;
print FILE $line;
}
print FILE @time;
close(FILE);
Upvotes: 1
Views: 126
Reputation: 6531
Try this:
assuming that $newh
, $newmin
and $newsec
are new values for hours, minutes and seconds respectively.
$line =~ s/\d+:\d\d:\d\d/$newh:$newmin:$newsec/g;
You can also combine the "new" variables in one:
my $new_time="$newh:$newmin:$newsec";
$line =~ s/\d+:\d\d:\d\d/$new_time/g;
Explanations:
\d+
- one or more decimal digits
:
- just a colon
\d
- exactly one decimal digit
Upvotes: 1