Inserting lines into text files can only be done by rewriting the text files. You will have to create a new file, write the three lines into it and then append monthly.txt to that file, and then rename that file to monthly.txt.

Update: See the below node by Anonymous Monk for stuff I left out and additional hints.

Some untested code :

#/usr/bin/perl -w use strict; my $daily = "daily.txt"; my $monthly = "monthly.txt"; # These will hold all lines from the files my @daily_lines; my @monthly_lines; # First extract the last three lines from $daily local *DAILY; open DAILY, "< $daily" or die "Can't open $daily: $!\n"; @daily_lines = <DAILY>; close DAILY; @daily_lines = @daily_lines[-3..-1]; # Now, @daily_lines contains the three last lines from the # file named $daily # Now we will recreate the monthly file : local *MONTHLY; open MONTHLY, "< $monthly" or die "Can't open $monthly : $!\n"; @monthly_lines = <MONTHLY>; close MONTHLY; # And write the new file : open MONTHLY, "> $monthly.tmp"; print MONTHLY join "", @daily_lines; print MONTHLY join "", @monthly_lines; close MONTHLY; # Now we delete $monthly and move our temp file over it : unlink $monthly or die "Couldn't remove old file $monthly : $!\n"; rename( "$monthly.tmp", "$monthly" ) or die "Couldn't rename $monthly. +tmp to $monthly : $!\n";


In reply to Re: Reading and writing to files by Corion
in thread Reading and writing to files by damian

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.