Re: Putting a line in a specific place in a file
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Mon, 06 Nov 2006 23:49:05 GMT
samasama wrote:
anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:
samasama <bryan@xxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
[positioning a file handle]
This finds the lines correctly but if I try to print after $gpg_line,Well, you can't.
the line below gets fubar.
I know I could use Tie::File, but i really want to understand what's
going on and how to insert my line after $gpg_line correctly.
A file is really a sequence of bytes, no more, no less. You can
append to the end of a file, but writing anywhere else implies
overwriting what's already there.
Duh... Yeah I feel dumb : P I don't know why I thought I could just
print the line.
Pity we can no longer refer to the FAQ for this question.
Perlfaq5 "How do I change one line in a file/delete a line..." used to
explain this problem in detail. These days it just refers to Tie::File,
another tool that makes files look like a sequence (a Perl array) of lines.
Yeah, Tie::File is great, but I needed to learn the steps in writing my
own parser, for future projects.
While, I've figured out what I needed to do and I'll paste the last of
the code below... I still don't understand what if ( ( $. == $base_line
.. /gpgkey\=/i ) =~ /E/ ) { translates too.
It looks like $. == $base_line go down using .. until we hit gpgkey,
and I have no idea what the /E/ means. I'd really like to understand
that.
Perhaps a demonstration may help:
$ seq 10 18 | perl -lne' my $x = 3 .. /16/; print "Line Number: $. Contents:
$_ Flip-flop: $x" '
Line Number: 1 Contents: 10 Flip-flop:
Line Number: 2 Contents: 11 Flip-flop:
Line Number: 3 Contents: 12 Flip-flop: 1
Line Number: 4 Contents: 13 Flip-flop: 2
Line Number: 5 Contents: 14 Flip-flop: 3
Line Number: 6 Contents: 15 Flip-flop: 4
Line Number: 7 Contents: 16 Flip-flop: 5E0
Line Number: 8 Contents: 17 Flip-flop:
Line Number: 9 Contents: 18 Flip-flop:
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.
- Follow-Ups:
- Re: Putting a line in a specific place in a file
- From: samasama
- Re: Putting a line in a specific place in a file
- References:
- Putting a line in a specific place in a file
- From: samasama
- Re: Putting a line in a specific place in a file
- From: John W. Krahn
- Re: Putting a line in a specific place in a file
- From: samasama
- Re: Putting a line in a specific place in a file
- From: samasama
- Re: Putting a line in a specific place in a file
- From: anno4000
- Re: Putting a line in a specific place in a file
- From: samasama
- Putting a line in a specific place in a file
- Prev by Date: force IV to NV
- Next by Date: Huge Memory Load for reading into memory
- Previous by thread: Re: Putting a line in a specific place in a file
- Next by thread: Re: Putting a line in a specific place in a file
- Index(es):
Relevant Pages
|