onemojofilter has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to take a spool file and replacing a string in a specific line, position within that file based on values found in other places in the file. As an example:
File sample contents:
H BIN LOC W123 CUSTOMER 12345 ABC BOTTLE COMPANY ITEM DESCRIPTION 24/12 OZ FC ABC 12-12 x 9-12 x 6-15 123 MY COMPANY PRODUCT # 12341234 20221103 QUANTITY PER PALLET ORDER QUANTITY 500 10,000 CUST. PO. NUMBER MY COMPANY ORDER NUMBER INVENTORY 961123-01 DATE PRINTED SHIP DATE 10/31/2022 11/03/2022 FREIGHT AREA SAL Load # 1 OF 21 ^LH BIN LOC W123 CUSTOMER 12345 ABC BOTTLE COMPANY ITEM DESCRIPTION 24/12 OZ FC ABC 12-12 x 9-12 x 6-15 123 MY COMPANY PRODUCT # 12341234 20221103 QUANTITY PER PALLET ORDER QUANTITY 500 10,000 CUST. PO. NUMBER MY COMPANY ORDER NUMBER INVENTORY 961123-01 DATE PRINTED SHIP DATE 10/31/2022 11/03/2022 FREIGHT AREA SAL Load # 1 OF 21
Firstly, the first line will display something like an "H" and each subsequent "H" in the file will be preceded by "^L" (which signifies a new "page") Secondly, I will take the value found on line 10 (relative to where "H" appears") on position 25 through 35 (in this case 20221103) and replace it with another value based upon the value found on line 14, positions 25 through 35. (Basically a database lookup, which I can do).
Google searches seem to only mention either using 'sed' (which I can readily do in a shell script, but am trying to do this in a more efficient and readable way using Perl) or looping through the file (which I'm already doing in a shell script). Is the only way to change a specific line/position in a file really going to be looping through the entire file?
I was hoping there was a method by which I could specify a line and a position and extract (and replace as needed - writing that portion back to the file - sort of how 'sed -i' would do, but something more perlish?) that instead of slurping the entire file.
Any thoughts/suggestions appreciated!
|
|---|