rahulme81 has asked for the wisdom of the Perl Monks concerning the following question:
I am matching a pattern in my file. for example search for 4567 and replace the 200.0.0.0 to 500.0.0.0, so that line looks now 4567 DEF 500.0.0.0.1234 ABC 100.0.0.0 4567 DEF 200.0.0.0 .....
What i need to do : Show the line before change and after change on screen and ask for user confirmation and proceed with other things later. Please advicemy $file = "$file_path/file.lst"; my $newid = "500.0.0.0" open MAST, $file or die "Unable to open file.lst: $!"; my $new = "$file.tmp.$$"; my $bak = "$file.bak"; open(NEW, "> $new") or die "can't open $new: $!"; while (<MAST>) { my ($pattern,$id) = (split /\s+/, $_)[0,4]; print $_; if ( $_ =~ m/^$pattern/ ) { $_ =~ s/$id/$newid/g; } (print NEW $_) or die "can't write to $new: $!"; } close(MAST) or die "can't close $file: $!"; close(NEW) or die "can't close $new: $!"; rename($file, $bak) or die "can't rename $file to $bak: $!"; rename($new, $file) or die "can't rename $new to $file: $!";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl file editing
by choroba (Cardinal) on Nov 04, 2015 at 10:02 UTC | |
|
Re: Perl file editing
by Athanasius (Archbishop) on Nov 04, 2015 at 10:22 UTC |