Good morning:

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!


In reply to Changing string in specific line/position in a file by onemojofilter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.