[33047] in Perl-Users-Digest
Perl-Users Digest, Issue: 4323 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 5 18:09:24 2014
Date: Fri, 5 Dec 2014 15:09:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 5 Dec 2014 Volume: 11 Number: 4323
Today's topics:
Re: Can you point me in the right direction - accessing <hjp-usenet3@hjp.at>
Re: Can you point me in the right direction - accessing <hjp-usenet3@hjp.at>
Re: Can you point me in the right direction - accessing <jblack@nospam.com>
Re: Can you point me in the right direction - accessing <jblack@nospam.com>
Re: Can you point me in the right direction - accessing <jblack@nospam.com>
Re: Dedup script is finished and works. Critiques? <gamo@telecable.es>
Re: Dedup script is finished and works. Critiques? <justin.1410@purestblue.com>
Re: Dedup script is finished and works. Critiques? <justin.1410@purestblue.com>
Re: Dedup script is finished and works. Critiques? <see.my.sig@for.my.address>
Re: Dedup script is finished and works. Critiques? <see.my.sig@for.my.address>
Re: Dedup script is finished and works. Critiques? <gamo@telecable.es>
Re: Dedup script is finished and works. Critiques? <jblack@nospam.com>
Re: Dedup script is finished and works. Critiques? <kaz@kylheku.com>
Read/write specific lines? <tuxedo@mailinator.com>
Re: Read/write specific lines? <gamo@telecable.es>
Re: Read/write specific lines? <rweikusat@mobileactivedefense.com>
Re: Read/write specific lines? <jurgenex@hotmail.com>
Re: Read/write specific lines? <bauhaus@futureapps.invalid>
Re: Read/write specific lines? <tuxedo@mailinator.com>
Re: Read/write specific lines? <gravitalsun@hotmail.foo>
Re: Read/write specific lines? <tuxedo@mailinator.com>
Re: Read/write specific lines? <gravitalsun@hotmail.foo>
Re: Read/write specific lines? <rweikusat@mobileactivedefense.com>
Re: Read/write specific lines? <gravitalsun@hotmail.foo>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 5 Dec 2014 19:18:19 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Can you point me in the right direction - accessing a website
Message-Id: <slrnm83tnb.mpn.hjp-usenet3@hrunkner.hjp.at>
On 2014-12-01 03:51, Luca <luca_remove@alice.it> wrote:
> On 11/30/2014 04:28 PM, John Black wrote:
>> I am relatively good with basic Perl but don't know web stuff. I
>> don't need to build a website (right now). All I want to do is have
>> a script that accesses a certain webpage, like my bank, and logs in
>> via a user id and pwd and then grabs some data like current balance.
[...]
>> bet there are existing cpan modules that do things like that.
WWW::Mechanize and friends.
>> It does not have to be specific to any application - just something
>> that can pull in a page and log in to it and then navigate around a
>> little to grab info.
>
> Not really a Perl answer, but...have you considered headless browsers?
> zombie.js and phantom.js come to mind. They are programmable through
> JavaScript.
Or through Perl if you use the right module, for example
WWW::Mechanize::PhantomJS (haven't used that myself yet).
hp
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
------------------------------
Date: Fri, 5 Dec 2014 19:27:41 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Can you point me in the right direction - accessing a website
Message-Id: <slrnm83u8t.mpn.hjp-usenet3@hrunkner.hjp.at>
On 2014-12-03 22:20, gamo <gamo@telecable.es> wrote:
> El 03/12/14 a las 22:34, Henry Law escribió:
>> On 03/12/14 19:49, gamo wrote:
>>> I'm using Ubuntu. How do you install modules without cpan?
>>
>> In general the module Foo::Bar is in a package called libfoo-bar-perl,
>> but you can usually find it using "apt-cache search foo-bar".
>>
>> Thus XML::Simple is libxml-simple-perl, DBI is libdbi-perl,
>> Finance::Quote is libfinance-quote-perl ... and so on.
>>
>> (PS how you do this with a graphic front-end I don't know 'cos I'm old
>> enough to be a command-line jockey. I'm sure you'll find out how.)
>
> This is good and bad. It's good because it's good for Perl use,
> but is bad because that people takes time to pack things already
> packed, which can be spent i.e. in packing a new version of jed, or
> reporting to cpan authors that their module doesn't install, or...
Overall its always a win if one person takes the time to (re)package
some software over thousands to millions of people trying to install it
from source.
From a systems administrator's view, installing a package from CPAN on
50 servers[1] is much more work than installing it via the operating
system's install mechanisms - and it will have all the dependencies,
will be upgraded automatically, and can be removed safely. If there is
no deb or rpm package, I need to make one myself.
> Ps: I'm supposing that they do not automate the production of
> libxyz-123-perl, in which case I'm totally wrong.
It's semi-automatic. You can produce a deb or rpm package from a cpan
package automatically. But often you want to change some details.
hp
[1] 50 servers is small. Try to do on 1000 servers. Or 100000.
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
------------------------------
Date: Fri, 5 Dec 2014 15:39:31 -0600
From: John Black <jblack@nospam.com>
Subject: Re: Can you point me in the right direction - accessing a website
Message-Id: <MPG.2eebe20a495bf637989807@news.eternal-september.org>
In article <m5psrg$7dm$1@dont-email.me>, lws4art@gmail.com says...
>
> Henry Law wrote:
> > On 03/12/14 23:20, John Black wrote:
> >> However, I think I may have gotten in over my head.
> >
> > You are now in a maze of twisty little passages, all alike.
> >
> > Travelling along this road, somewhat in front of you, I have found that
> > this isn't unusual. For example, I've written a simple Mechanize
> > program that logs in to a router and grabs the up and down counts. Going
> > to http://wherever gives one page which contains a redirect to another
> > page, in which javascript summons up yet another page which is the one
> > the user sees. I have resorted to line tracing to figure it out. Screen
> > scraping (for that is what we're both doing) was always difficult and
> > painstaking.
> >
> > Try turning javascript off on your browser, emptying the cache and going
> > to the login screen at your bank; is what you see different from normal?
> > If so then save the HTML of that page and look for redirects and
> > javascript weirdness. Code up the key parts of that in your Mechanize
> > program and try again.
> >
> > What worries me is that all this trial-and-error is firing repeated HTTP
> > requests at your bank, which may spook them (it ought to), to say
> > nothing of getting your password locked out.
> >
>
> It also is just a *Bad Idea ?* from the get-go. Fraught with all kinds
> of issues and a security risk. As I stated elsewhere banks typically
> have automated email/text alerts that you can setup to notify you of
> your current account status. Those would be PUSHed from them and not
> PULLed from you and eliminate the login/security issue.
This bank (Chase) does not give me what I need. I.e. I can't tell it to send me the current
balance every morning. I also wanted to grab 2 or maybe 3 different balances and create one
daily report and send it to multiple email addresses. I can easily do everything I want
except get in the front door...
John Black
------------------------------
Date: Fri, 5 Dec 2014 15:43:01 -0600
From: John Black <jblack@nospam.com>
Subject: Re: Can you point me in the right direction - accessing a website
Message-Id: <MPG.2eebe2e58307c631989808@news.eternal-september.org>
In article <LYqdnWMhgap4jx3JnZ2dnUVZ8nydnZ2d@giganews.com>, news@lawshouse.org says...
> On 03/12/14 23:20, John Black wrote:
> > However, I think I may have gotten in over my head.
>
> You are now in a maze of twisty little passages, all alike.
>
> Travelling along this road, somewhat in front of you, I have found that
> this isn't unusual. For example, I've written a simple Mechanize
> program that logs in to a router and grabs the up and down counts. Going
> to http://wherever gives one page which contains a redirect to another
> page, in which javascript summons up yet another page which is the one
> the user sees
I was under the impression that Mechanize should automatically follow redirects. But it does
not handle javascript. Someone pointed me to WWW::Mechanize::PhantomJS which I have not
gotten to look at yet.
John Black
------------------------------
Date: Fri, 5 Dec 2014 15:48:12 -0600
From: John Black <jblack@nospam.com>
Subject: Re: Can you point me in the right direction - accessing a website
Message-Id: <MPG.2eebe41487064933989809@news.eternal-september.org>
In article <M9CdnQs0Ud_zbx3JnZ2dnUVZ572dnZ2d@giganews.com>, see.my.sig@for.my.address says...
>
> On 11/30/2014 1:28 PM, John Black wrote:
>
> > Hi,
> >
> > I am relatively good with basic Perl but don't know web stuff. I don't need to build a
> > website (right now). All I want to do is have a script that accesses a certain webpage, like
> > my bank, and logs in via a user id and pwd and then grabs some data like current balance. I
> > know how to make the program run once a day so that my computer can automatically stay on top
> > of my account and send me the daily balance or other alerts. Can someone point me in the
> > right direction? I'd bet there are existing cpan modules that do things like that. It does
> > not have to be specific to any application - just something that can pull in a page and log
> > in to it and then navigate around a little to grab info. Thanks.
> >
> > John Black
>
> I can tell you right now that's probably not going to work.
> Many banks have anti-bot features such as "captcha" to block
> just that kind of thing. And many will ask a random security
> question from a set of several ("what was the name of your
> childhood pet?") if they don't recognize your current
> browser as one you've used before. So you'd have to write
> a perl script that recognizes all of the security questions
> its likely to see, knows all the right answers, and is able
> to enter the correct answers into the correct fields of the
> web forms and hit "enter" or click "ok" for you.
This is indeed depressing. But I have not given up. If I can make my script look
sufficiently like my browswer, then I can use my browser to ingratiate itself with the
security and then the bank is hopefully happy for a few months such that the script does not
have to deal with captcha or security questions. Its a theory...
>
> Alternative idea: research "AutoIt3"; that might be closer
> to what you need than perl. (But even an AutoIt3 script is
> going to have a hard time with Captcha.)
I'll look into this if I keep hitting dead ends for too long. Thanks.
John Black
------------------------------
Date: Fri, 05 Dec 2014 08:03:42 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <m5rlcc$daa$1@speranza.aioe.org>
El 05/12/14 a las 01:13, Robbie Hatley escribió:
> 4. Perhaps I should present each cluster of several duplicates
> to user all at once, instead of one pair at a time, and give
> more options as to what to do?
Sure. You have to hear what people says you before coding a
useless script.
If you have 7 dupes, you present the same interrogation
(7*7-7)/2 times, instead of just 6 times. That's wrong.
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Fri, 5 Dec 2014 09:32:52 +0000
From: Justin C <justin.1410@purestblue.com>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <4na8lb-r9r.ln1@zem.masonsmusic.co.uk>
On 2014-12-05, Robbie Hatley <see.my.sig@for.my.address> wrote:
> # No sense trying to compare directories byte-for-byte, seeing as how
> # they aren't files and don't HAVE bytes to compare, so if the current
> # "file" is actually a directory, just move on to the next file:
> if ( -d _ ) {next;}
Why 'stat' it, and set $ModData and $ModTime, if you're only going
to skip it? Test for this first... alternatively, you could to
test "if ( -f _ )" because you don't want to do this exercise on
a pipe, a socket or a symbolic link.
In addition you should probably check it's readable or your won't
be able to compare it with any other file.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Fri, 5 Dec 2014 09:26:40 +0000
From: Justin C <justin.1410@purestblue.com>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <gba8lb-r9r.ln1@zem.masonsmusic.co.uk>
On 2014-12-05, Robbie Hatley <see.my.sig@for.my.address> wrote:
> my ($dev, $ino, $mode, $nlink, $uid,
> $gid, $rdev, $size, $atime, $mtime,
> $ctime, $blksize, $blocks)
> = stat($FileName);
my ($Size, $mtime) = (stat $FileName)[7,9];
Justin.
--
Justin C, by the sea.
------------------------------
Date: Fri, 05 Dec 2014 03:42:22 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <Hvudnb9PNq-HBxzJnZ2dnUVZ57ydnZ2d@giganews.com>
On 12/4/2014 11:03 PM, gamo wrote:
> El 05/12/14 a las 01:13, Robbie Hatley escribió:
>> 4. Perhaps I should present each cluster of several duplicates
>> to user all at once, instead of one pair at a time, and give
>> more options as to what to do?
>
> Sure. You have to hear what people says you before coding a
> useless script.
I find the script *very* useful, already nearly as powerful as
the C++ version I wrote some 11 years ago, but much shorter
and simpler. I'm surprised. I'd thought of Perl as being
a "lower level" language than C++, but I think now that it's
actually higher-level.
As for the *semantics* of this program, it was never much open
for debate; I already knew exactly what I wanted the program
to do, so I wasn't too much interested in "hearing what people
say" about that.
But as for the *implementation*, I was and am all ears. My thanks
to the folks here who have offered advice in that regard.
> If you have 7 dupes, you present the same interrogation
> (7*7-7)/2 times, instead of just 6 times. That's wrong.
Given a set of 7 dupes, the user will be presented with
a maximum of 6+5+4+3+2+1 = 21 pairs. However, if the user
deletes one file each time he's presented with a pair of
dupes, only 6 presentations will be made, not 21 (because
after erasing 6 of 7, there's nothing left to compare the
winning candidate against!).
(Unless I've made some error that would allow presenting
files that no longer exist... but no, when I open each file
I say "or next FIRST" or "or net SECOND", so we skip
file names that can't be opened because they no longer exist.)
An alternate way would be to present the set of files
serialized like so:
The following files are duplicates of each other:
1. Jack's Regular Expression Tutorial.pdf
2. Jack's Regex Tutorial.pdf
3. REGEXTUTORIAL.PDF
4. Goleta-Summer.jpg
5. jacksretut.pdf
6. re.pdf
7. jack_re_tutorial.pdf
Which (if any) files do you want to delete? Type a list
of serial numbers separated by spaces, then hit Enter.
To ignored these duplicates, just hit Enter.
To quit, type "q" and hit Enter.
1 3 4 5 6 7
Erased Jack's Regular Expression Tutorial.pdf
Erased REGEXTUTORIAL.PDF
Erased Goleta-Summer.jpg
Erased jacksretut.pdf
Erased re.pdf
Erased jack_re_tutorial.pdf
I might do something like that. Doesn't require unbuffered
keyboard input that way, so it would be more portable.
And it gives the user a birds eye view of the file names
of all the duplicates, so it's easier to pick the winning
candidate on the basis of "better name" and kill all the
others.
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
------------------------------
Date: Fri, 05 Dec 2014 04:00:42 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <GaudnXfNcMD7AxzJnZ2dnUVZ57ydnZ2d@giganews.com>
On 12/5/2014 1:26 AM, Justin C wrote:
> On 2014-12-05, Robbie Hatley <see.my.sig@for.my.address> wrote:
>> my ($dev, $ino, $mode, $nlink, $uid,
>> $gid, $rdev, $size, $atime, $mtime,
>> $ctime, $blksize, $blocks)
>> = stat($FileName);
>
> my ($Size, $mtime) = (stat $FileName)[7,9];
Ah, yes, that's much cleaner.
Or even just:
my $Size = (stat $FileName)[7];
since I'm not actually using mod time.
My file record hash then becomes a
"hash of references to arrays of scalars"
instead of a
"hash of references to arrays of references to hashes"
But on second thought, no, I'll leave the time and date info
in the inner hashes, because I think I'll implement a feature
my C++ version has, which is to offer the user the choice of
"erasing all newer duplicates without further prompting",
which requires time and date to determine which file is
newer.
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
------------------------------
Date: Fri, 05 Dec 2014 13:11:45 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <m5s7dv$r20$1@speranza.aioe.org>
El 05/12/14 a las 12:42, Robbie Hatley escribió:
> An alternate way would be to present the set of files
> serialized like so:
>
> The following files are duplicates of each other:
> 1. Jack's Regular Expression Tutorial.pdf
> 2. Jack's Regex Tutorial.pdf
> 3. REGEXTUTORIAL.PDF
> 4. Goleta-Summer.jpg
> 5. jacksretut.pdf
> 6. re.pdf
> 7. jack_re_tutorial.pdf
> Which (if any) files do you want to delete? Type a list
> of serial numbers separated by spaces, then hit Enter.
> To ignored these duplicates, just hit Enter.
> To quit, type "q" and hit Enter.
Better, but you have to rewrite the script.
Good luck.
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Fri, 5 Dec 2014 11:14:02 -0600
From: John Black <jblack@nospam.com>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <MPG.2eeba3d5513d7296989806@news.eternal-september.org>
In article <m8-dnRjSztYPZR3JnZ2dnUVZ572dnZ2d@giganews.com>, see.my.sig@for.my.address says...
> I finished that "find duplicate files" program I was writing as
> a "learn Perl" exercise,
>
Thanks for sharing this program and experience. Its been an educational discussion!
John Black
------------------------------
Date: Fri, 5 Dec 2014 17:27:56 +0000 (UTC)
From: Kaz Kylheku <kaz@kylheku.com>
Subject: Re: Dedup script is finished and works. Critiques?
Message-Id: <20141205092104.527@kylheku.com>
On 2014-12-05, Robbie Hatley <see.my.sig@for.my.address> wrote:
>
> I finished that "find duplicate files" program I was writing as
> a "learn Perl" exercise, and it appears to work correctly, after
> correcting a few amusing run-time bugs. Such as, it was comparing
> each file to itself, saying "blat.txt is a duplicate of blat.txt;
> which would you like to erase, blat.txt or blat.txt?" :-)
Your program wastes time comparing files that may be the same inode
on disk: the same object. Take advantage of identical $ino fields.
Moreover, in that situation, there is often a good reason for
the multiple entries for the same file. Someone or some program
wanted the file in two or more places, without taking up space.
The user should be informed that some foo/X and bar/Y are not just identical,
but are in fact the same object.
> 1. The way I'm reading unbuffered single characters from keyboard
> is ...
... completely unnecessary in a utility for finding duplicates.
Even if you must have interactive features, you can read lines of input
from the user, like "git {commit | reset | add | stash save} --patch".
------------------------------
Date: Fri, 5 Dec 2014 13:50:55 +0100
From: Tuxedo <tuxedo@mailinator.com>
Subject: Read/write specific lines?
Message-Id: <m5s9ng$8ui$1@news.albasani.net>
Hello,
I have a file with two lines, for example:
75
line two
I would like read from and write to the first line only, something along:
open(FILE, "<file") or die("Could not open to read $!");
my $value = <FILE>; # but read only first line with the number?
# read the second line?
close(FILE);
$value++; # modify the value found on the first line
open(FILE, ">file") or die("could not open to write $!");
print FILE $value;
# write first line, as above, then copy the second unmodified line below?
close(FILE);
How can the first, second or any other specified line be read and thereby
have modifications made to specific line numbers?
In the above example I presume one way is to read the whole file and modify
line one and thereafter write back the whole and partly modified file.
Any examples or ideas would be much appreciated.
Many thanks,
Tuxedo
------------------------------
Date: Fri, 05 Dec 2014 14:46:28 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Read/write specific lines?
Message-Id: <m5scvi$a7u$1@speranza.aioe.org>
El 05/12/14 a las 13:50, Tuxedo escribió:
> Hello,
>
> I have a file with two lines, for example:
>
> 75
> line two
>
> I would like read from and write to the first line only, something along:
>
> open(FILE, "<file") or die("Could not open to read $!");
> my $value = <FILE>; # but read only first line with the number?
> # read the second line?
>
> close(FILE);
>
> $value++; # modify the value found on the first line
>
> open(FILE, ">file") or die("could not open to write $!");
> print FILE $value;
> # write first line, as above, then copy the second unmodified line below?
>
> close(FILE);
>
> How can the first, second or any other specified line be read and thereby
> have modifications made to specific line numbers?
>
> In the above example I presume one way is to read the whole file and modify
> line one and thereafter write back the whole and partly modified file.
>
> Any examples or ideas would be much appreciated.
>
> Many thanks,
> Tuxedo
>
The simplest way: A) read one file, modify, write other file
then, B) perldoc -f rename; perldoc -f unlink or even File::Copy
HTH
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Fri, 05 Dec 2014 15:01:11 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Read/write specific lines?
Message-Id: <87vblqjg3s.fsf@doppelsaurus.mobileactivedefense.com>
Tuxedo <tuxedo@mailinator.com> writes:
> I have a file with two lines, for example:
>
> 75
> line two
>
> I would like read from and write to the first line only, something along:
>
> open(FILE, "<file") or die("Could not open to read $!");
> my $value = <FILE>; # but read only first line with the number?
> # read the second line?
>
> close(FILE);
>
> $value++; # modify the value found on the first line
>
> open(FILE, ">file") or die("could not open to write $!");
> print FILE $value;
> # write first line, as above, then copy the second unmodified line below?
>
> close(FILE);
>
> How can the first, second or any other specified line be read and thereby
> have modifications made to specific line numbers?
As addition to what gamo already wrote: It's not possible to do this
in-place because a file is basically an array of bytes which means
insertion or deletion of a byte at position ... requires changing the
positions of all subsequent bytes. Further, a text file has no structure
existing independently of its contents: The only way to determine record
(line) boundaries is to do a lexical analysis of it aka 'scan through
all the data in order to find the linebreaks'.
Because of this, any modification of a single line requires copying all
lines in front of it to 'an output file', then, writing the modified
data to that, then, copying all the lines behind the modified one to the
output file, possibly followed by renaming it such that it replaces the
input file.
If your modifications are relatively simple, it may be possible to
accomplish them with what's already built into perl. Eg, assuming the
following input file (named afile)
---------
This is line 1.
This is line 2.
This is line 3.
This is line 4.
---------
running
perl -i -lpe '$. == 3 and $_ = "This is a Martian invasion! Run for your life!!"' afile
changes line 3 such that the file now looks like this:
---------
This is line 1.
This is line 2.
This is a Martian invasion! Run for your life!!
This is line 4.
---------
------------------------------
Date: Fri, 05 Dec 2014 07:46:58 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Read/write specific lines?
Message-Id: <gmk38a1qvpis4hqdghbcabpgau38rbu1db@4ax.com>
Tuxedo <tuxedo@mailinator.com> wrote:
>I have a file with two lines, for example:
>
>75
>line two
>
>I would like read from and write to the first line only, something along:
[...]
>How can the first, second or any other specified line be read and thereby
>have modifications made to specific line numbers?
Your Question is Asked Frequently, please see
perldoc -q "change, delete, or insert a line in a file"
jue
------------------------------
Date: Fri, 05 Dec 2014 16:49:59 +0100
From: "G.B." <bauhaus@futureapps.invalid>
Subject: Re: Read/write specific lines?
Message-Id: <m5sk6m$hig$1@dont-email.me>
On 05.12.14 16:01, Rainer Weikusat wrote:
> Further, a text file has no structure
> existing independently of its contents:
Certainly, in many popular file systems and given that
popular I/O layers are designed with streams in mind,
absence of external structure from text files is likely
always true. It seems, though, that Perl could handle
text files that were stored in VMS filesystems which
features record-oriented I/O.
http://en.wikipedia.org/wiki/List_of_file_systems#Record-oriented_file_systems
------------------------------
Date: Fri, 5 Dec 2014 17:16:26 +0100
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Read/write specific lines?
Message-Id: <m5sloq$361$1@news.albasani.net>
Jürgen Exner wrote:
[...]
>
> Your Question is Asked Frequently, please see
> perldoc -q "change, delete, or insert a line in a file"
>
> jue
I had a feeling it was a frequently asked/answered question.
Thanks for the perldoc pointer and for the earlier above responses, which
in all should put me on the right track.
Tuxedo
------------------------------
Date: Fri, 05 Dec 2014 19:51:32 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Read/write specific lines?
Message-Id: <m5srb5$2f6g$1@news.ntua.gr>
>
> Any examples or ideas would be much appreciated.
>
# what you asked
open FILE, '+<', 'file.txt' or die "oups $!\n";
my $previous = <FILE>;
seek FILE, 0, 0;
print FILE ++$previous;
close FILE;
------------------------------
Date: Fri, 5 Dec 2014 22:46:43 +0100
From: Tuxedo <tuxedo@mailinator.com>
Subject: Re: Read/write specific lines?
Message-Id: <m5t944$apu$1@news.albasani.net>
George Mpouras wrote:
> >
> > Any examples or ideas would be much appreciated.
> >
>
> # what you asked
> open FILE, '+<', 'file.txt' or die "oups $!\n";
> my $previous = <FILE>;
> seek FILE, 0, 0;
> print FILE ++$previous;
> close FILE;
Many thanks for the above although I don't see through the code entirely. I
will save the example for testing another time.
Meanwhile, some of what is detailed in perldoc -q "change, delete, or
insert a line in a file" did the trick!
And perhaps suited for smaller purposes and files, I found assigning my
@value = <FILE> list and modify specified lines also to work, e.g.:
$value[0]++;
.. and then print line-by-line:
print FILE "$value[0]\n";
print FILE "$value[1]\n";
etc.
Tuxedo
------------------------------
Date: Fri, 05 Dec 2014 23:57:22 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Read/write specific lines?
Message-Id: <m5t9o3$1orr$1@news.ntua.gr>
>
> I don't see through the code entirely. I
operator +< is for read and write at the same time .
read what you want either line by line or go directly where you want
with seek , and then write whatever wherever you want with seek.
With this trick you will open the file once for both read/write.
------------------------------
Date: Fri, 05 Dec 2014 22:01:51 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Read/write specific lines?
Message-Id: <87d27x4uy8.fsf@doppelsaurus.mobileactivedefense.com>
Tuxedo <tuxedo@mailinator.com> writes:
[...]
> And perhaps suited for smaller purposes and files, I found assigning my
> @value = <FILE> list and modify specified lines also to work, e.g.:
> $value[0]++;
> .. and then print line-by-line:
> print FILE "$value[0]\n";
> print FILE "$value[1]\n";
> etc.
Well, of course it does.
@value = <FILE>;
evaluates <FILE> in list context. The cause perl to read all if it and
split it into lines (in a single pass), then, return a list of all these
lines. This list is then assigned to the array @value. Instead of
printing @values 'line by line', you can as well just do
print FILE (@values)
if the newlines where left in place or
print FILE (map { $_, "\n" } @values)
if they weren't.
------------------------------
Date: Sat, 06 Dec 2014 00:33:15 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Read/write specific lines?
Message-Id: <m5tbrb$22gq$1@news.ntua.gr>
an other idea that fits well with your numerical lines.
create the file.txt
winter
100
spring
200
summer
300
autumn
400
and then run the code for fun
local $^I = '.bak';
local @ARGV='file.txt';
while (<>) {
chomp;
s/^(\d+)$/(1+$^N)/e;
print "$_\n"
}
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4323
***************************************