Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

insert into beginning of file

by rchou2 (Novice)
on Jul 22, 2002 at 13:39 UTC ( #184035=perlquestion: print w/replies, xml ) Need Help??

rchou2 has asked for the wisdom of the Perl Monks concerning the following question:

hi, i need to insert/print at the beginning of a file without deleting the file.....anyone has tips on that?

Replies are listed 'Best First'.
Re: insert into beginning of file
by Abigail-II (Bishop) on Jul 22, 2002 at 13:41 UTC
    Use Tie::File.

    Abigail

      Supplementary to Abigail-II's advice here's example code of how you might insert a line into the beginning of a file using Tie::File
      use strict; use Tie::File; tie(my @fl, 'Tie::File', 'somefile.txt') or die("ack - $!"); unshift @fl, 'a line of text here'; untie @fl;
      If you're not doing line-based insertion into your file then your best choice is to open a new file and write to that.
      HTH

      _________
      broquaint

        When using Tie::File, I get the following error: Can't locate Tie/File.pm Is this because I don't have the latest version of Perl?
Re: insert into beginning of file
by jmcnamara (Monsignor) on Jul 22, 2002 at 14:07 UTC

    I'd recommend Tie::File as well (compare the following code to the example shown by broquaint above). Nevertheless, here is a simple example using traditional methods:
    #!/usr/bin/perl -w use strict; # Open the file in read mode open FILE, "file.txt" or die "Error message here $!"; # Slurp in the file my $str = do{local $/; <FILE>}; close FILE or die "Error message here $!"; # Reopen the file in write mode (clobbers old file) open FILE, ">file.txt" or die "Error message here $!"; # Add some text print FILE "Prepend this text.\n"; # Add the old contents of the file. print FILE $str; close FILE or die "Error message here $!";
    This is similar except that the file is only opened once (in read/write mode):
    #!/usr/bin/perl -w use strict; # Open the file for reading and writing open FILE, "+<file.txt" or die "Error message here $!"; # Slurp in the file my $str = do{local $/; <FILE>}; # Rewind the file seek FILE, 0, 0; # Add some text print FILE "Prepend this text.\n"; # Add the old contents of the file. print FILE $str; close FILE or die "Error message here $!";

    Both of these examples assume that the data in the file isn't too large to be slurped into memory. If it is then you can modify the code to use an intermediary file.

    --
    John.

Re: insert into beginning of file
by fsn (Friar) on Jul 22, 2002 at 15:07 UTC
    Tie::File is of course the way to do it. The old, hardcore, quickhack, low memory, no extra module, the-way-they-used-to-do-it-in-the-80's way is:

    #!/usr/bin/perl open INFILE, "<infile"; open OUTFILE, ">tmpfile"; # Create your temporary names with care, +or die a painful race-condition death. print OUTFILE qq!Here you print the new stuff. ... !; while (<INFILE>) { print OUTFILE $_; } close INFILE; close OUTFILE;

    and then rename the tempfile.
Re: insert into beginning of file
by zentara (Archbishop) on Jul 22, 2002 at 17:21 UTC
    Here's a method similar to using Tie::File, but dosn't require
    a module. It dosn't delete....it overwrites :-)
    #!/usr/bin/perl $file= $ARGV[0]; @file =(<>); unshift @file, "My newline headline\n"; open (FH,">$file")or die "Cant open: $!"; print FH @file; close FH;
Re: insert into beginning of file
by flounder99 (Friar) on Jul 22, 2002 at 15:39 UTC
    One other thing should be mentioned is the -i command line switch. It specifies that using <> operator will edit files in place with an optional extension to rename the input file. See perlrun for the details.

    I mentioned this before here.

    This will add text to the beginning of temp.txt and back up the current contents of temp.txt to temp.txt.bak

    perl -pi.bak -e 'unless ($notfirst++){print "text to add\n";}' temp.t +xt

    --

    flounder

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://184035]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2023-01-27 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?