Write line at beginning of file?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Re: Write line at beginning of file?

    Eric Pement <pemente@northpark.edu> wrote:
    > [email]tadmc@augustmail.com[/email] (Tad McClellan) wrote in message news:<slrnbfe194.mf6.tadmc@magna.augustmail.com>.. .
    >> perldoc -q beginning
    >>
    >> How do I change one line in a file/delete a line in a
    >> file/insert a line in the middle of a file/append to the
    >> beginning of a file?
    > Some people may disagre, but I found the Perl FAQ disappointing in
    > this answer. I have a recent ActiveState Perl,

    What version of perl are you referring to?

    > and the FAQ says this:
    >
    > Although humans have an easy time thinking of a text file as
    > being a sequence of lines that operates much like a stack of
    > playing cards--or punch cards--computers usually see the text
    > file as a sequence of bytes. In general, there's no direct way
    > for Perl to seek to a particular line of a file, insert text
    > into a file, or remove text from a file.

    That is not what it says in the most recent version of perl (5.8.0).

    (but I liked that old answer better than the new answer...)

    > Huh? Maybe the authors mean something by "direct way" that I'm not
    > understanding properly.

    Yes, the question says "a file" multiple times.

    > I think the question can be answered directly.
    > $. is the current line number under default circumstances:
    >
    > perl -pe 'print "Prepended line\n" if $.==1;' infile >outfile

    That is *two* files, not "a file".

    _True_ inplace editing is not straightforward, so the FAQ suggests
    the effect of inplace editing (but it isn't really "in place", it
    is in a _different_ place, a temp file).

    > It works on Perl v5.6.1, at any rate.

    And the FAQ says to do just that, perhaps you did not recognize
    that that is what was being said?

    The general solution is to create a temporary copy of
    the text file with the changes you want, then copy that
    over the original.
    ...
    Perl can do this sort of thing for you automatically with the
    ...

    > The questioner should know not
    > to put more than one file on the command line for this one-liner.

    How command line redirection works depends on the shell, not on
    the application (perl). That would be a shell answer, not a perl answer.

    > If
    > he wants to process several files, it's probably better to use a shell
    > script

    The Perl FAQ should answer questions in Perl, not cop-out
    to using something else. :-)

    You can do multiple in-place edits from a single Perl program
    as well as (better, actually) from a shell program.

    > with the -i switch to perform
    > in-place replacement,

    Errr, that _is_ what the FAQ you quoted recommends, so what
    was wrong with that answer again?

    :-)


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  2. Similar Questions and Discussions

    1. vi, adding words to the beginning of each line
      in vi-ing a file, i have a list of lines and they have different alphabets when they begin a new line. now i wanted to add this "more" to the...
    2. ( I need help) How can i write in a second line in a sting?
      i Have this script : -- writestring(obj, "Your points for Activity 1 is: ") -- writestring(obj,member("vva1").text) I am trying to save...
    3. What does a semicolon do at the beginning of a line?
      Was browsing the documentation on reading a configuration file and found this. What does a semicolon do at the beginning of a line? ; <?php DO...
    4. Add text to beginning every line
      Hi Hope someone can help with this. I used to be able to do it, but now have forgotten. I want to prepend some text to every line in a file. The...
    5. placing .swf file and the beginning of dir movie
      Hai, There is a simple lingo u could use. say an swf file occupying in director time line from 10 to 40 Then u could write this code at any...
  3. #2

    Default Re: Write line at beginning of file?

    [email]pemente@northpark.edu[/email] (Eric Pement) writes:
    > Some people may disagre, but I found the Perl FAQ disappointing in
    > this answer.
    Why?
    > I have a recent ActiveState Perl, and the FAQ says this:
    >
    > Although humans have an easy time thinking of a text file as
    > being a sequence of lines that operates much like a stack of
    > playing cards--or punch cards--computers usually see the text
    > file as a sequence of bytes. In general, there's no direct way
    > for Perl to seek to a particular line of a file, insert text
    > into a file, or remove text from a file.
    >
    > Huh? Maybe the authors mean something by "direct way" that I'm not
    > understanding properly.
    To directly modify a file means to, errr..., directly _modify_ _the_
    _file_.
    > I think the question can be answered directly.
    > $. is the current line number under default circumstances:
    >
    > perl -pe 'print "Prepended line\n" if $.==1;' infile >outfile
    That is indirect. It does not directly prepend a line to an existing
    file. It produces a new file which must then be renamed over the
    original file to give the effect of prepending - as illustrated in the
    FAQ.
    > ... the -i switch to perform in-place replacement ...
    It is still indirect. All it does is automate the renaming as is
    explained in the FAQ.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

  4. #3

    Default Re: Write line at beginning of file?

    [email]tadmc@augustmail.com[/email] (Tad McClellan) wrote in message news:<slrnbfhgda.2im.tadmc@magna.augustmail.com>.. .
    > Eric Pement <pemente@northpark.edu> wrote:
    > > [email]tadmc@augustmail.com[/email] (Tad McClellan) wrote in message news:<slrnbfe194.mf6.tadmc@magna.augustmail.com>.. .
    >
    >
    > >> perldoc -q beginning
    > >>
    > >> How do I change one line in a file/delete a line in a
    > >> file/insert a line in the middle of a file/append to the
    > >> beginning of a file?
    [ ... ]
    > > and the FAQ says this:
    > >
    > > Although humans have an easy time thinking of a text file as
    > > being a sequence of lines that operates much like a stack of
    > > playing cards--or punch cards--computers usually see the text
    > > file as a sequence of bytes. In general, there's no direct way
    > > for Perl to seek to a particular line of a file, insert text
    > > into a file, or remove text from a file.
    >
    >
    > That is not what it says in the most recent version of perl (5.8.0).
    >
    > (but I liked that old answer better than the new answer...)
    Me too. Let me say one thing in response. I have maintained a FAQ
    file for several years, and I'm accustomed to hearing lots of simple
    questions that are tough for the questioner but are very basic for me.

    I think the nature of the question requires a different kind of
    response or detail, not the same level of complexity or sophistication
    for all questions. When a person asks "How do I add a line to the top
    of a file?", I think they need a simple, no-frills answer with a
    minimum of complexity. When a person asks a question about socket
    programming, a greater degree of complexity can be admitted into the
    answer. In short, make the answer appropriate to the question. Thus,
    for a very basic question, even presuming that the questioner knows
    how to use Perl modules could be going too far.

    [ ... ]
    > _True_ inplace editing is not straightforward, so the FAQ suggests
    > the effect of inplace editing (but it isn't really "in place", it
    > is in a _different_ place, a temp file).
    [ ... ]
    > And the FAQ says to do just that, perhaps you did not recognize
    > that that is what was being said?
    >
    > The general solution is to create a temporary copy of
    > the text file with the changes you want, then copy that
    > over the original.
    > ...
    > Perl can do this sort of thing for you automatically with the
    > ...
    To be honest, I was reacting more to the paragraphs that preceded
    this one than to this paragraph itself, which addresses it more
    directly.

    [ ... ]
    > You can do multiple in-place edits from a single Perl program
    > as well as (better, actually) from a shell program.
    >
    >
    > > with the -i switch to perform
    > > in-place replacement,
    >
    >
    > Errr, that _is_ what the FAQ you quoted recommends, so what
    > was wrong with that answer again?
    >
    > :-)
    Thanks for the smile. You and I were both presuming (correctly,
    I think) that the Original Poster had not read the FAQ. I'm the greater
    skeptic and I think the OP may not even have known *how* to look up the
    FAQ, so I repeated the recommendation about the -i switch in the wild
    hope that the OP might actually return to c.l.p.m. to see if anyone had
    answered his question! I just try to do my best. :)

    Thanks again for your response.

    --
    Eric Pement
    Eric Pement Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139