[25296] in Perl-Users-Digest
Perl-Users Digest, Issue: 7541 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Dec 20 11:15:53 2004
Date: Mon, 20 Dec 2004 08:15:17 -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 Mon, 20 Dec 2004 Volume: 10 Number: 7541
Today's topics:
Re: Quick easy question... <jgibson@mail.arc.nasa.gov>
Re: Regex help - need flexibility to parse with or with <jgibson@mail.arc.nasa.gov>
Re: replace multiple lines in a file with Perl <jgibson@mail.arc.nasa.gov>
Re: search/replace and update the file in perl script <invalid@rochester.rr.com>
selecting STDOUT AND STDERR <bill@gites.org.uk>
Solution to touble of Win32::MSGBOX (Koala)
Re: Solution to touble of Win32::MSGBOX <spamtrap@dot-app.org>
Re: Solution to touble of Win32::MSGBOX <1usa@llenroc.ude.invalid>
Re: why the following HereDoc print don't work? <spamtrap@dot-app.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 14 Dec 2004 09:17:05 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Quick easy question...
Message-Id: <141220040917059136%jgibson@mail.arc.nasa.gov>
In article <1103039875.310902.203610@c13g2000cwb.googlegroups.com>,
big.e12 <big.e12@gmail.com> wrote:
> I am just getting started in perl and I was wondering if someone could
> help me out... I am trying to write a script that counts the number of
> times a word appears in the input. Can anyone help me?
Quick easy hint: m/\bword\b/g
See also
perldoc -q count
perldoc perlre # search for \\b
perldoc perlop # search for PATTERN
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
------------------------------
Date: Thu, 16 Dec 2004 15:47:39 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Regex help - need flexibility to parse with or without blanks in output....
Message-Id: <161220041547391380%jgibson@mail.arc.nasa.gov>
In article <1103230773.649120.82730@f14g2000cwb.googlegroups.com>,
<tudmuf2b@onebox.com> wrote:
> Perl Gurus:
>
> I'm trying to write a script that runs the rup command, and then takes
> the output of it to do something.
>
> Here are 2 possible outputs that rup could give you (if a box has been
> up for less than a day, there is no "xx days" field):
>
>
> bastion1 up 18:01, load average: 0.17 0.15 0.06
> bastion2 up 30 days, 22:34, load average: 0.00 0.00 0.02
>
>
> My script works for the 2nd case (the "bastion2" host) - even if "days"
> is singular. But how do I make my regex more flexible so that it can
> say:
>
> 1. if the box has been up for less than a day, then just skip to the
> time field and grab it, so that $days=0, $time=18:01 (from the example
> above).
>
> 2. if the box has been up for X days, then I want to grab the number of
> days so that $days=30, and also grab the time, so that $time=22:34
> (above).
>
> Somehow I need to say something like
> /.*up\s+(\d+\s+days?,|.*\d{1,2}:\d+),/
>
> but I want to grab $days no matter what:
Why not just write two regexs?
#!/usr/local/bin/perl
#
use strict;
use warnings;
while(<DATA>) {
my($days,$time) = (0,0);
if( /(\d+)\s*days,\s*(\d{1,2}:\d{2})/ ) {
$days = $1;
$time = $2;
}elsif( /up\s*(\d{1,2}:\d{2})/ ) {
$time = $1;
}
print "days=$days, time=$time\n";
}
__DATA__
bastion1 up 18:01, load average: 0.17 0.15 0.06
bastion2 up 30 days, 22:34, load average: 0.00 0.00 0.02
Output:
days=0, time=18:01
days=30, time=22:34
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
------------------------------
Date: Thu, 16 Dec 2004 11:13:16 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: replace multiple lines in a file with Perl
Message-Id: <161220041113167457%jgibson@mail.arc.nasa.gov>
In article <1103218888.568856.289030@z14g2000cwz.googlegroups.com>,
learner <tzhai2002@yahoo.com> wrote:
> Ok, Please do these steps to run the program I posted:
> 1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
> 2. in the same directory, create a new directory called 'changed'
> 3. create a new file with whatever name you want, say it is called
> 'oldtxt', which contains the oldtxt that you like to replace (the same
> txt listed in the program)
> 4. then you issue command :
>
> perl rep.pl oldtxt
> to run it.
> Hope this will help, and see if you can help me out in return. Thanks.
>
Here is a program that demonstrates multi-line replacement and can be
cut-and-pasted-and-run by anyone. This is what Tad and Paul are asking
for so they can help you. Notice that I have removed all references to
external files and used the special <DATA> file handle to include a
file at the end of the program. Notice also the 'use warnings' and 'use
strict' lines. I have also removed the copyright symbol, as I am not
sure how to include that in the data file. The presence of special
characters may cause a problem for your replacement, but it shouldn't
be causing the multiple replacement problem you are having. Perhaps
using this program as a starting point will help.
Please read and adhere to the posting guidelines for this newsgroup if
you wish to get further help here. Please try to get ahold of a real
newsreader for posting, as Google is removing whatever indenting you
had in the code you posted (you did have some indenting, didn't you?)
Thanks.
---%<-----------------------------------------
#!/usr/local/bin/perl
#
use warnings;
use strict;
undef $/;
my $original = "
// This is an old text
// that is across several
// lines in this file. This file
// also contains the copy right
// symbol.";
my $replacement = "
// I just want to replace
// the old lines in one fell
// swoop!";
my $data=<DATA>;
print "\ndata:\n$data\n";
if ($data =~ s/$original/$replacement/g) {
print "\nstring changed:\n$data\n";
}else{
print "\nstring not changed\n";
}
__DATA__
// This is an old text
// that is across several
// lines in this file. This file
// also contains the copy right
// symbol.
__OUTPUT__
data:
// This is an old text
// that is across several
// lines in this file. This file
// also contains the copy right
// symbol.
string changed:
// I just want to replace
// the old lines in one fell
// swoop!
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
------------------------------
Date: Thu, 16 Dec 2004 22:21:31 -0500
From: Bob Walton <invalid@rochester.rr.com>
Subject: Re: search/replace and update the file in perl script
Message-Id: <41c2503d$1_1@127.0.0.1>
jrefactors@hotmail.com wrote:
> I need a perl script to search and replace the keywords in a file, and
> update that file with new
> keyword.
>
> The usage should be perl modify.pl oldvalue newvalue filename
>
> I wrote the script, I can search and replace, but I guess
> the problem is I don't know how to update the file.
>
> #!/usr/bin/perl
You're missing:
use warnings;
use strict;
Let Perl give you all the help it can.
> $oldValue = $ARGV[0];
> $newValue = $ARGV[1];
> $fileName = $ARGV[2];
>
> open(FILE, $fileName) or die("Unable to open file");
You should include the variable $! in your die string somewhere so that
you receive a clue as to why the open failed.
>
> # read file into an array
> @data = <FILE>;
>
> foreach $line (@data)
> { print $line;
You are printing $line before you modify it. Then you modify it and
never use it again. You probably want your print after your substitute
statement below. Don't forget to add the semicolon statement terminator
if you do that. Note also that the output of the print will go to
STDOUT, and can be placed in a file by output redirection. If you want
to actually modify the original file, there are several options. One is
to check out the -i switch when running Perl. In conjunction with the
-p switch, you can probably get all this down to a couple of lines. If
you want to modify the original file in place without the -i switch,
you'll need to read it like you did, then close the filehandle and
reopen for writing, then write the modified file contents.
> $line =~ s/$oldValue/$newValue/
Note that if $oldValue contains regexp metacharacters you may get
behavior other than what you might desire. If you want metacharacters
in $oldValue to be treated like any other character, use \Q to quote any
metacharacters:
$line =~ s/\Q$oldValue/$newValue/;
Also, if your $oldValue string could appear more than once in a line and
you want to replace all occurrences, you should add the /g switch at the
end of the subsitition operator, as:
$line =~ s/\Q$oldValue/$newValue/g;
Also note carefully that $line is an alias for each element of @data in
turn in your foreach loop, and that modifying $line line also modifies
the contents of @data. For what you are doing, this is irrelevant, but
if your real program uses @data later, you would find its contents modified.
> }
Finally, note that for what you are doing, it is not necessary to read
the entire contents of the file into memory -- that will fail if the
file is appropriately huge. Line-by-line processing is preferred where
possible. You can write out to a new temporary file, then, when the
write is complete, rename to a backup filename or unlink the original
file and rename the temporary file to the original file name.
--
Bob Walton
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
------------------------------
Date: Mon, 20 Dec 2004 12:59:02 +0100
From: "Bill Parker" <bill@gites.org.uk>
Subject: selecting STDOUT AND STDERR
Message-Id: <41c6be87$0$22305$8fcfb975@news.wanadoo.fr>
I'm used to doing this
exec >>${LOG} 2>&1
In ksh and bash
And I know in perl I can do
open(LOG,">>logfile");
$stdout = select(LOG)
print "something\n";
select($stdout);
But can I exactly mimic the ksh/bash redirect of stderr as well as stdout?
And do I need to? Does perl distinguish betwixt stdout and stderr? What does
"die" write to? (s'pose I ought to test that out and find out for myself) -
I guess I'm concerned about my silently-running script failing in some way
and anything going to stderr not being picked up in the logfile...
My script is actually running on Windoze using WInXP's awful task scheduler
rather than cron so I can't redirect via the calling command... and anyway,
I prefer my scripts to be fully in control of things - I don't like hunting
down a long complicated chain of things that affect my proggies' execution.
As ever, all help gratefully received.
Cheers
Bill
------------------------------
Date: 20 Dec 2004 06:06:26 -0800
From: chris_reydon@hotmail.com (Koala)
Subject: Solution to touble of Win32::MSGBOX
Message-Id: <d8fcddc.0412200606.3f0270a8@posting.google.com>
Hello
the probleme is simple, you must inhib the pile overflow vérification
of your anti virus, if you use Virus Scan 8.0.
Good Luck
------------------------------
Date: Mon, 20 Dec 2004 09:23:01 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Solution to touble of Win32::MSGBOX
Message-Id: <cZGdnWICpvDYfVvcRVn-rw@adelphia.com>
Koala wrote:
> Hello
>
> the probleme is simple
What problem are you talking about? If you're replying to a message,
*REPLY* to it, don't start a new thread.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 20 Dec 2004 14:57:10 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Solution to touble of Win32::MSGBOX
Message-Id: <Xns95C5653F431CFasu1cornelledu@132.236.56.8>
chris_reydon@hotmail.com (Koala) wrote in news:d8fcddc.0412200606.3f0270a8
@posting.google.com:
> the probleme is simple, you must inhib the pile overflow vérification
> of your anti virus, if you use Virus Scan 8.0.
Please do not post gibberish. Please don't start new threads when you
should be replying to the original thread.
> Good Luck
Seems like you are going to need it more.
Sinan.
------------------------------
Date: Mon, 20 Dec 2004 06:33:11 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: why the following HereDoc print don't work?
Message-Id: <Y9qdnbwiMbDlJVvcRVn-jQ@adelphia.com>
Sherm Pendley wrote:
> I'm going to verify this with the latest Perl before I file a perlbug -
> I need to upgrade anyway. And I'll try to be a good citizen and follow
> it up with a patch to p5p, if I can manage to create one.
Okay, I've verified that the bug still exists in 5.8.6, and used
'perlbug' to file a bug. (Haven't received a verification or response
yet though - should I expect one?)
No luck with the patch. I found the likely function in toke.c, but the
code in there is scary. Perl's lexer is apparently *not* the best place
to begin studying the internals... :-(
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 V10 Issue 7541
***************************************