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

Fellow Monks:
I need to add 1 line to the beginning of a file.
How do I do this in perl through a script, preferably not a command-line one-liner??
Your wisom is greatly appreciated. Thanks.
  • Comment on adding a line to the beginning of a file

Replies are listed 'Best First'.
Re: adding a line to the beginning of a file
by tlm (Prior) on Apr 26, 2005 at 19:35 UTC

    This is a FAQ.

    use Tie::File; tie my @lines, 'Tie::File', 'myfile.txt' or die 'tie failed'; unshift @lines, "a new line\n"; untie @lines;

    the lowliest monk

Re: adding a line to the beginning of a file
by ww (Archbishop) on Apr 26, 2005 at 20:00 UTC
    Puh-lese!!
    • Do you care what's in the added line? (or would a solution using random content work/be required?)
    • How are you defining line?
      (A collection of chars followed by a \n? ...and, then how are you defining "\n"?
      or maybe...
      a string of not_more_than 65|72|80 chars?)
      ...And oh, yeah, we may need to know encoding, ...
    • To what sort of file are you adding this line: text or binary?
    • OS?

    • And WHAT HAVE YOU READ? WHAT HAVE YOU TRIED?

    Suggest you read How do I post a question effectively?, perldoc, the FAQs here, and maybe search for "prepend" "filehandle" or....

    Enough.
    Here's a simple, less sarcastic reply: We don't do other people's homework (not "knowingly" anyway) and we greatly prefer to spend time answering questions where post shows that the poster has made some effort (eg, has written and posted code; has provided a clear and unambiguous indication of the expected/desired results, etc.)
    So go ahead and make our day. Demonstrate some effort and you'll find rich rewards here.

      You Sarcastic Wretch!
      No! this was not homework. It was merely a simple question of which knowing not where to begin.
      It is your type of high and mighty demeanor and sarcasm that scares off those who indeed may need help the most. In the time you wasted in spewing out sarcasm you likely could have used your energy in coming up with an effective response such as the more noble monk tlm did in his response. Often times the "lowliest monk"(s), as tlm referred to himself have the best responses. Thanks TLM.
      Perhaps a lesson of humility is in order for those who think that they are too high and mighty giving sarcasm instead of an effective more useful response.
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: adding a line to the beginning of a file
by TedPride (Priest) on Apr 26, 2005 at 19:58 UTC
    Depends. What size is the file? Can the entire thing be safely loaded into memory at once, or do you want to edit it in small pieces? If the former, it's just a matter of loading the file into a string or array and then printing it back with an extra line put at the beginning. If the latter, you open a new file under a different name from the original, write your new line to it, then read chunks from the old file and print them to the new file until you run out of chunks. Unlink the old file, rename the new file.

    We can give more specific code when we have some information about why you're trying to do this.

Re: adding a line to the beginning of a file
by metaperl (Curate) on Apr 27, 2005 at 16:26 UTC
Re: adding a line to the beginning of a file
by trammell (Priest) on Apr 27, 2005 at 19:38 UTC
    No need to use Perl:
    % echo "See spot run" > newfile % cat origfile >> newfile % mv newfile origfile
    Voila!