Ask a Question related to PERL Miscellaneous, Design and Development.
-
Tad McClellan #1
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
-
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... -
( 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... -
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... -
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... -
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... -
Brian McCauley #2
Re: Write line at beginning of file?
[email]pemente@northpark.edu[/email] (Eric Pement) writes:
Why?> Some people may disagre, but I found the Perl FAQ disappointing in
> this answer.
To directly modify a file means to, errr..., directly _modify_ _the_> 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.
_file_.
That is indirect. It does not directly prepend a line to an existing> 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
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.
It is still indirect. All it does is automate the renaming as is> ... the -i switch to perform in-place replacement ...
explained in the FAQ.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
Eric Pement #3
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?
Me too. Let me say one thing in response. I have maintained a FAQ>> > 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...)
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).
To be honest, I was reacting more to the paragraphs that preceded> 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
> ...
this one than to this paragraph itself, which addresses it more
directly.
[ ... ]
Thanks for the smile. You and I were both presuming (correctly,> 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?
>
> :-)
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



Reply With Quote

