oryan has asked for the wisdom of the Perl Monks concerning the following question:
I need to find and replace 2 lines of code related to culverts in a model text file. I have a model with all the lines in the right order with the OLD culvert values, and another text file with the NEW culvert values but in the wrong order. What the script does currently:
1. In the model, finds line beginning with text "Connection Culv". This is the line of text I need to replace.
2. Finds the next line after "Connection Culv" that starts with "Conn Culvert Barrel" - this is the unique identifier for the replacement.
3. Pulls new values of "Connection Culv" from text file and replaces them in model.
4. Repeats for all instances of Connection Culv and then saves new file.
Instead of ONLY replacing the line that begins with "Connection Culv" I need it to replace that line and the following line ( 111 111, 222 222, etc), but I can't get it to work.
EXAMPLE:
MODEL IN CORRECT ORDER:Connection Culv=This is Line1 111 111 Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 222 222 Conn Culvert Barrel=Culvert2 * Connection Culv=This is Line3 333 333 Conn Culvert Barrel=Culvert3 *
Connection Culv=This is Line3 - New text here 333 333 This should be new too Conn Culvert Barrel=Culvert3 * Connection Culv=This is Line1 - New text here 111 111 This should be new too Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 - New text here 222 222 This should be new too Conn Culvert Barrel=Culvert2 *
Connection Culv=This is Line1 - New text here 111 111 Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 - New text here 222 222 Conn Culvert Barrel=Culvert2 * Connection Culv=This is Line3 - New text here 333 333 Conn Culvert Barrel=Culvert3 *
Connection Culv=This is Line1 - New text here 111 111 This should be new too Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 - New text here 222 222 This should be new too Conn Culvert Barrel=Culvert2 * Connection Culv=This is Line3 - New text here 333 333 This should be new too Conn Culvert Barrel=Culvert3 *
There are hundreds of these replacements that need to be made throughout the model. I feel like this should be simple, but nothing has worked. Here is the code I have currently that works for replacing the single line "Connection Culv" but not the following line. Any help is appreciated. Thanks.
# HEC-RAS Replacement Perl Script # This will find and replace values in the HEC-RAS geometry file for m +odified culvet barrels. The process is: # 1. In existing model file (HECRAS_Ex.txt) find where there is a " +Connection Culv" (this is the line that needs to be replaced) # 2. It then down for the next Conn Culvert Barrel line (this is th +e unique identifier) # 3. It then takes from the new culvert file (culvNEW.txt) the new +"Connection Culv" line and replaces it in the existing HECRAS_Ex.txt +file. # 4. Repeats for all and then saves out Output_HECRAS.txt # Nomenclature for running Perl Script: # C:\MyDir> perl PERL_SCRIPT.pl culvNEW.txt HECRAS_Ex.txt OutPut_H +ECRAS.txt # Read Existing HEC-RAS Geometry File (HECRAS_Ex) with Old Culvert Con +nection Attributes open (TEMPLATE, @ARGV[1]) or die; @HECRAS_Ex = <TEMPLATE>; close TEMPLATE; # Read New Culvert Data File (culvNEW) with new Connection culvert Att +ributes open (TEMPLATE, @ARGV[0]) or die; @culvNEW = <TEMPLATE>; close TEMPLATE; for ($i=0; $i<@HECRAS_Ex; $i++) { # only check lines starting with "Connection Culv" in the HECRAS_Ex fi +le if ($HECRAS_Ex[$i] =~ /^Connection Culv/) { #print $HECRAS_Ex[$i]; #look for Connection Culv backwards $iback=$i-1; while ($HECRAS_Ex[$iback] !~ /^Conn Culvert Barrel/) { $iback=$iback+1; } $local0=$HECRAS_Ex[$iback]; chomp($local0); # print $HECRAS_Ex[$iback]; for ($j=0; $j<@culvNEW; $j++) { # for ($j=0; $j<1; $j++) { $local = $culvNEW[$j]; chomp($local); # print $local; # Remove the trailing new line # chomp $local; # print ($local eq $HECRAS_Ex[$iback]); if ($local =~ /^$local0/) { # print "match"; $jforward=$j-1; while ($culvNEW[$jforward] !~ /^Connection Culv/) { $jforward=$jforward-1; } # print $culvNEW[$jforward]; # Perform substitutions of LG card $HECRAS_Ex[$i]=$culvNEW[$jforward]; # print $HECRAS_Ex[$i]; } } } } #write out the Geometry File based on the HECRAS_Ex file structure and + the new values in the culvNEW file open (OUT, ">" . @ARGV[2]) or die; # Write output print OUT @HECRAS_Ex; # Close OUT close OUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Find and replace based on unique identifier
by tybalt89 (Monsignor) on Aug 20, 2018 at 21:04 UTC | |
|
Re: Find and replace based on unique identifier
by Cristoforo (Curate) on Aug 20, 2018 at 19:22 UTC | |
by oryan (Initiate) on Aug 20, 2018 at 20:28 UTC |