Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How do I add a few stings of text, 30 lines before the EOF?

by Anarion (Hermit)
on Oct 16, 2001 at 12:32 UTC ( [id://119084]=note: print w/replies, xml ) Need Help??


in reply to How do I add a few stings of text, 30 lines before the EOF?

You have to count all the lines of the file, you can use $. for that, heres an example withoout reading all file in an array:

#!/usr/bin/perl -w use strict; open(FILE,"file.txt") or die "Cant open file.txt: $!"; 1 while <FILE>; my $line=$.-30; $.=0; open(FILE,"file.txt") or die "Cant open file.txt: $!"; open(FILE2,">file2.txt") or die "Cant create file2.txt: $!"; while(<FILE>) { print FILE2 $_; insert(*FILE2) if $.==$line; } rename("file2.txt","file.txt") or die "Cant rename file: $!"; sub insert { my $fh=shift; print "Inserting at line $line\n"; print $fh "$_\n" for 0 .. 15; }


$anarion=\$anarion;

s==q^QBY_^=,$_^=$[x7,print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://119084]
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: (4)
As of 2024-04-25 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found