Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Find and replace based on unique identifier

by Cristoforo (Curate)
on Aug 20, 2018 at 19:22 UTC ( [id://1220738]=note: print w/replies, xml ) Need Help??


in reply to Find and replace based on unique identifier

Hi oryan,

Here is a solution that prints out the new file in correct order and doesn't use the existing file at all. It sets the input record separator to $/ = "*\n" and reads in 'chunks' of the file. Also, I found a trailing space at the end of each line which I removed prior to parsing the file. If your data does have these trailing spaces, then they would need to be accounted for. I'm assuming they aren't in the 'new' file, but got there by copy and paste.

The solution uses a Schwartzian transform to process the blocks.

#!/usr/bin/perl use strict; use warnings; open my $new, '<', \<<EOF; 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 * EOF $/ = "*\n"; print map {$_->[0]} sort {$a->[1] <=> $b->[1]} map {[$_, /^Conn Culvert Barrel=Culvert(\d+)$/m]} <$new>;
Output was:
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 *

Replies are listed 'Best First'.
Re^2: Find and replace based on unique identifier
by oryan (Initiate) on Aug 20, 2018 at 20:28 UTC

    Thank you for the reply. I provided a very simplified, striped down version of the model itself. There are thousands of lines of code and there are hundreds of these lines that I need to replace scattered throughout the model, so unfortunately sorting is not an option. Sorry if I was not clear on that in my initial post. Here is a small example of the actual model itself - there are 3 examples of the culvert line that I would need to replace in this snipit (#1. Conn Culvert Barrel=1,Cul1,0, #2. Conn Culvert Barrel=2,Cul1,0, #3. Conn Culvert Barrel=1,Cul2,0). Thanks again for any help:

    BreakLine Name=BL1 BreakLine CellSize Min= BreakLine CellSize Max= BreakLine Near Repeats=1 BreakLine Polyline= 2 749229.01307448720208.606197753 749310.99413914719981.581711002 Connection=Connection1 ,749170.3458749,719393.4466371 Connection Desc= Connection Line=4 749264.197132839719397.961390208749158.302314896719392.527851194 749148.302367149719392.560178611 749076.48011895719389.280834652 Connection Last Edited Time=Aug/17/2018 12:55:44 Conn Near Repeats=1 Connection Up Reach= Connection Up River= Connection Up RS= Connection Dn Reach= Connection Dn River= Connection Dn RS= Conn Routing Type= 1 Conn Use RC Family=False Conn OverFlow Method 2D=False Conn Weir WD=10 Conn Weir Coef=3 Conn Weir Is Ogee= 0 Conn Simple Spill Pos Coef=0.05 Conn Simple Spill Neg Coef=0.05 Conn Weir SE= 18 01500.45630.77757 1500.7940.786091500.95655.810461501.13160.817 +041501.216 65.823611501.27975.836771501.38685.849921501.55495.863081501.659100.86 +971501.775 116.03411502.304120.88151502.426135.89721502.754140.90241502.841152.95 +841503.119 179.7221503.689185.9493 1503.88187.93121503.923 Connection Culv=2,3,10,100.69,0.02,0.3,1,8,1,1002,1001, 1 ,Group1B + , 0 , 100 100 Conn Culvert Barrel=1,Cul1,0 Conn Culv Bottom n=0.02 Connection Culv=1,3,,100.69,0.02,0.3,1,2,3,1000.01,999.01, 1 ,Group1A + , 0 , 115 115 Conn Culvert Barrel=2,Cul1,0 Conn Culv Bottom n=0.02 Conn Outlet Rating Curve= 0 ,False,, Connection=Connection2 ,749150.8027089,720183.502415 Connection Desc= Connection Line=3 749212.113799506720183.551667985749148.471637979720183.975109164 749089.500952284720182.466598541 Connection Last Edited Time=Aug/17/2018 12:56:39 Conn Near Repeats=1 Connection Up Reach= Connection Up River= Connection Up RS= Connection Dn Reach= Connection Dn River= Connection Dn RS= Conn Routing Type= 1 Conn Use RC Family=False Conn OverFlow Method 2D=False Conn Weir WD=10 Conn Weir Coef=3 Conn Weir Is Ogee= 0 Conn Simple Spill Pos Coef=0.05 Conn Simple Spill Neg Coef=0.05 Conn Weir SE= 15 01505.09818.654211505.91923.654321506.17528.654431506.16133.654 +541505.944 58.65511505.82883.661761505.80188.66339 1505.7493.665021505.73598.666 +661505.694 103.66831505.722108.66991505.781113.67161505.877118.67321506.027122.63 +35 1506.19 Connection Culv=4,3,4,79.64,0.02,0.3,1,29,2,1505,1504.5, 1 ,Group2 + , 0 , 50 50 Conn Culvert Barrel=1,Cul2,2 749148.417384509720223.797157772749148.525891449720144.153060555 Conn Culv Bottom n=0.02 Conn Outlet Rating Curve= 0 ,False,, LCMann Time=Dec/30/1899 00:00:00 LCMann Region Time=Dec/30/1899 00:00:00 LCMann Table=0 Chan Stop Cuts=-1 Use User Specified Reach Order=0 GIS Ratio Cuts To Invert=-1 GIS Limit At Bridges=0 Composite Channel Slope=5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found