perlynewby has asked for the wisdom of the Perl Monks concerning the following question:
I've got a little program that opens a file to read then writes it to a NEW file. I'll like to search out some keywords and replace them in the NEW file.
I am having trouble figuring out how to do a substitute string and then continue to write out the rest of the content with NEW edit in the NEW file.
#!usr/bin/perl # program takes a base part and changes some fields to show new part # use strict; #use warning; #use 5.280; use Data::Dumper; use Fcntl qw(:flock SEEK_END); use Cwd 'abs_path'; # Info from Base Part Job report my $base_part_test = '5237all5x_a1_rpt.txt'; my $base_part_file = '5237all5x'; my $base_part_file_uc = uc $base_part_file; my $base_part_rev = 'a1'; my $base_part_rev_lc = lc $base_part_rev; my $base_part_Customer = 'SA'; my $base_path = '/Product/SA_customer_product/'.$base_part_Customer.'/ +'.$base_part_file_uc.'/'.$base_part_file_uc.'_rev_'.$base_part_rev_lc +.'/RetBuildReport'; my $Real_base_part_file = $base_path.'/'.$base_part_file.'_'.$base_par +t_rev.'_rpt.txt'; #check if the Report file exists in the directory then do the substitu +tion of builder. if ( -f $Real_base_part_file) { # Info to New Buld Report my $New_part_report = 'NEW_PART_REPORT.txt'; # Test what's inside the vars print Dumper ( $base_part_file, $base_part_rev , $base_path, $Real_bas +e_part_file ); # Creating a file system "touch $New_part_report"; my $New_Part_Date = system "date"; open my $base_fh , '+<', $base_part_test or die; open my $New_fh ,'>', $New_part_report or die; while(my $line = <$base_fh>) { if ($line eq "Builder : Xi Wong"){ print " found original builder : "; Want to substitute builder Xi Wong to Lucca P. but not + sure how??? # then continue to write the rest of report print $New_fh $line; } } close($base_fh); close($New_fh); }
These are the substitution I am trying to change on the fly. If you can give me an idea of doing one substitution then I can do the rest of them. I just need some help to go about it
Original base part to start. This File gets read. In his case, Builder is Xi Wong but can be others
--------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 02 + Builder: Xi Wong Part Name: MG5237ALL5X + Customer Name: SI Route: S5H2-12A
This shows only the builder substitution from old to new. In this case, it is Lucca P. but can be others
--------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 01 + Builder: Lucca P. Part Name: MG5415DP + Customer Name: SA Route: S5H2-12A
|
|---|