Ingenious Perl Monks, I'm trying to search for the word "END" in a file (which occurs only once), then replace it with several lines of text. I am having trouble in doing this correctly. I tried reading and writing from the same file, but get incorrect results. I am slurping all of the data to be inserted instead of "END" into one scalar, then trying to add that in place. The code that I am currently using is here:
$COMPFILE = IO::File->new; #!!!!!!!!!!!!!!!!!!!!!!!!! SCALAR SLURP !!!!!!!!!!!!!!!!!!!!!!!! $comptmplt_file="comp_template.txt"; { local $/; open SLURP, $comptmplt_file or die "sudden death"; $comptmplt=<SLURP>; close SLURP or die "cannot close $comptmplt: $!"; } #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! $COMPFILE->open("+< comp_no_rods.txt" or die "Can't open"); print "$comptmplt"; until ($COMPFILE -> eof){ $compline = $COMPFILE->getline(); if ($compline=~ /END/){; $compline=~ s/END/$comptmplt/; print $COMPFILE "$compline"; } }
I think some error is occuring, because the text in the scalar replace variable has the word "END" in it as well, but the code doesn't get stuck in an infinite loop, it just adds some screwy lines along with the correct ones. I am sure I am doing this backwards and someone here has a better idea and I'm all for hearing it. Any further questions are welcomed as well. Thanks! Update: I figured it'd be easier if I posted what was in each file: The end of the file to be altered (note the "END" is what I want to change)
! ========================================================== TITLE T2 C1 2V-03-12100 9193 - 9406 EFPH. UTBR STEG NR 018 OPTION Burnup,Neu3,NoSearch,TTable,FlwTot,NoPowEnd PRINT KEYPAR INIT /sompath/dist-9193.dat=HIST SAVE /somepath/dist-9406.dat=POWER,HIST,TM,TIPNEU,PRMNEU,CRWDR, SAVE * PINPOW,PINBUR,PINLHR,PINFIM,PINDRY POWER 88.900 FLOW 7737.0 PRESS 70.000 CONROD ALL=100 BURNUP 213.0000, 1, EFPH, Straight-Euler END
The text to be inserted in place of the "END"
! ========================================================== TITLE T2 C1 2V-03-12100 BEGIN - FINISH EFPH. UTBR STEG NR 018 OPTION Burnup,Neu3,NoSearch,TTable,FlwTot,NoPowEnd PRINT KEYPAR INIT /somepath/dist-BEGIN.dat=HIST SAVE /somepath/dist-FINISH.dat=POWER,HIST,TM,TIPNEU,PRMNEU,CRWDR, SAVE * PINPOW,PINBUR,PINLHR,PINFIM,PINDRY POWER 100.0 FLOW 6500 PRESS 70.000 CONROD ALL=100 BURNUP 500.0000, 1, EFPH, Straight-Euler END
I will do this once, do some calculations, check to see if I have satisfied a criteria. If I haven't, I'll add more data to the file by changing the "END" in the exact same way above, with the file growing by another addition of data, then re-run a calculation. (Hopefully that makes sense). If you want to see the output I currently get, feel free to ask. Thanks again.

In reply to text file search and replace by igotlongestname

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.