Saved has asked for the wisdom of the Perl Monks concerning the following question:
#Should have total count of changes for file Thanx much for your time, and any help given. Bobuse strict; use warnings; # Get file to process (my $file, my $pattern, my $replacement) = @ARGV; my $Count = 0; # Read file open my $FH, "<", $file or die "Unable to open $file for read exited $ +? $!"; chomp (my @lines = <$FH>); close $FH; # Parse and replace text in same file open $FH, ">", $file or die "Unable to open $file for write exited $? +$!"; for (@lines){ say {$FH} $_ unless (/$pattern/); #Write to file unchanged say {$FH} $_ if (s/$pattern/$replacement/g); #Write to file change +d if ($_ =~ /$pattern/) { #Need to check & count multiple occur +rences in same line $Count = $Count + 1; #Need to increment correctly print $Count; #Wanted to test but will not print return $Count; #Need to get count back to main } } close $FH; print $Count;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: user input Regular Expression
by BillKSmith (Monsignor) on Dec 13, 2020 at 20:09 UTC | |
|
Re: user input Regular Expression (updated)
by haukex (Archbishop) on Dec 13, 2020 at 17:19 UTC | |
|
Re: user input Regular Expression -- oneliner
by Discipulus (Canon) on Dec 13, 2020 at 17:08 UTC | |
by eyepopslikeamosquito (Archbishop) on Dec 14, 2020 at 03:18 UTC | |
|
Re: user input Regular Expression
by jwkrahn (Abbot) on Dec 13, 2020 at 19:56 UTC | |
|
Re: user input Regular Expression
by jcb (Parson) on Dec 14, 2020 at 01:03 UTC |