[24170] in Perl-Users-Digest
Perl-Users Digest, Issue: 6362 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Apr 4 14:10:29 2004
Date: Sun, 4 Apr 2004 11:10:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 4 Apr 2004 Volume: 10 Number: 6362
Today's topics:
Re: Search and replace a string in files <lawshouse.public@btconnect.com>
Re: Search and replace a string in files <ittyspam@yahoo.com>
Re: Search and replace a string in files <ittyspam@yahoo.com>
Re: Search and replace a string in files (Himanshu Garg)
Re: Search and replace a string in files <tadmc@augustmail.com>
Re: Search and replace a string in files <tadmc@augustmail.com>
Re: test <gnari@simnet.is>
Re: test <uri@stemsystems.com>
Re: test <tadmc@augustmail.com>
Weird timezone problems on Win2k <cschadl@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 04 Apr 2004 12:17:33 +0100
From: Henry Law <lawshouse.public@btconnect.com>
Subject: Re: Search and replace a string in files
Message-Id: <firv605oajisvv53b50ulointcdtae140p@4ax.com>
On 3 Apr 2004 22:41:03 -0800, khoslakapil@yahoo.com (Kapil Khosla)
wrote:
>Essentially I am just converting all the occurences of Main to main in
>a file.
If you're doing this as an exercise then fine (and my Perl is nothing
like good enough to help you); but if you just need it done then maybe
some other tool would be better? I think a good sed-ist would do this
in one line.
Henry Law <>< Manchester, England
------------------------------
Date: Sun, 4 Apr 2004 10:06:10 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Search and replace a string in files
Message-Id: <20040404095918.T5768@dishwasher.cs.rpi.edu>
On Sun, 3 Apr 2004, Kapil Khosla wrote:
> I am new to PERL and I am trying to write a script which can do the
> following changes.
>
> int Main() CHANGES TO int main()
> #define Main main CHANGES TO #define main main
> int Main(String *str) CHANGES TO int main(String *str)
>
> Essentially I am just converting all the occurences of Main to main in
> a file.
>
> I read the chapter on Regular expressions and wrote the following
> script to do a search for the pattern with Main in an array with the
> same requirement.This works correctly for me.
>
> ##################
> use strict;
> my $i;
> my @arrstr = ('Mainframe','#define main Main','int Main(String
> ^sd)','int Main()','DllMain','int Main(ad)','MainMain');
>
> for( $i=0;$i<@arrstr;$i = $i+1 )
> {
> print "$arrstr[$i]\n";
>
> if ($arrstr[$i] =~ m{\bMain(\(.*\))?$} )
> {
> print "String matched\n\n";
That RegExp is far more complicated than it needs to be. You're searching
for: "Word boundary, followed by 'Main', then *optionally* followed by
parenthesized text, followed by end-of-line. Why do you care about the
parentheses and end of line? You said all you wanted to do was replace
"Main" with "main". So the match could just be:
m/\bMain/
> }
> }
>
> #################
>
> Now, I dont know how to use this in a file and replace the string
> matched with the corresponding lower case main.
> This is what I could write till now.
>
>
>
> open FILEHANDLE,"E:\\file.cpp" or die ("Could not open file for
> reading");;
>
> my @myfile = <FILEHANDLE>; # get the file in an arraylist
>
> for $line(@myfile)
> {
> # search for a pattern, if exists, replace with the correct str ?? #
> This is what I dont know how to do :(
> }
> close FILEHANDLE;
>
>
> Can you help?
You said you read the chapter on RegExps. Did you somehow skip over the
search-and-repalce operator? The line you're looking for is:
s/\bMain/main/;
HOWEVER, this is such a common task that your entire program can be
reduced to *one line* of Perl code:
perl -pi.bkp -e 's/\bMain/main/' filename.txt
(the .bkp of that line will create backups of your original file, just in
case you did something you didn't mean to.)
To understand how this works, do
perldoc perlrun
and search for the -p and -i switches.
Hope this helps,
Paul Lalli
------------------------------
Date: Sun, 4 Apr 2004 10:07:18 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Search and replace a string in files
Message-Id: <20040404100616.D5768@dishwasher.cs.rpi.edu>
On Sun, 4 Apr 2004, Henry Law wrote:
> On 3 Apr 2004 22:41:03 -0800, khoslakapil@yahoo.com (Kapil Khosla)
> wrote:
>
> >Essentially I am just converting all the occurences of Main to main in
> >a file.
>
> If you're doing this as an exercise then fine (and my Perl is nothing
> like good enough to help you); but if you just need it done then maybe
> some other tool would be better? I think a good sed-ist would do this
> in one line.
>
So would a good Perl programmer. :-)
perl -pi.bkp -e 's/Main/main/' file.txt
Paul Lalli
------------------------------
Date: 4 Apr 2004 07:30:49 -0700
From: himanshu@gdit.iiit.net (Himanshu Garg)
Subject: Re: Search and replace a string in files
Message-Id: <a46e54c7.0404040630.2d4b5ccc@posting.google.com>
khoslakapil@yahoo.com (Kapil Khosla) wrote in message news:<919aa2da.0404032241.191f4d6b@posting.google.com>...
> I am new to PERL and I am trying to write a script which can do the
> following changes.
>
> int Main() CHANGES TO int main()
> #define Main main CHANGES TO #define main main
> int Main(String *str) CHANGES TO int main(String *str)
>
> Essentially I am just converting all the occurences of Main to main in
> a file.
>
> I read the chapter on Regular expressions and wrote the following
> script to do a search for the pattern with Main in an array with the
> same requirement.This works correctly for me.
>
> ##################
> [Code that works on arrays]
> #################
>
> Now, I dont know how to use this in a file and replace the string
> matched with the corresponding lower case main.
> This is what I could write till now.
>
>
> open FILEHANDLE,"E:\\file.cpp" or die ("Could not open file for
> reading");;
>
> my @myfile = <FILEHANDLE>; # get the file in an arraylist
>
> for $line(@myfile)
> {
> #search for a pattern, if exists, replace with the correct str ??
> # This is what I dont know how to do :(
$line =~ s/Main/main/g; # substitute every occurence of Main
with main.
print $line;
> }
> close FILEHANDLE;
>
>
The above would give the output to Standard output. If you want to
edit the file in place use the following with the three arguments
source_string new_string file_names:-
use English;
use strict;
use Tie::File;
if(scalar(@ARGV) < 3)
{
print STDERR "usage: $PROGRAM_NAME source_string new_string
file_names";
die;
}
my $original = $ARGV[0];
my $new = $ARGV[1];
foreach my $i(2 .. $#ARGV)
{
my @array;
tie @array, 'Tie::File', $ARGV[$i] or
die "cannot tie file $ARGV[$i],
$OS_ERROR";
for(@array)
{
s/$original/$new/g;
}
}
> Can you help?
> Thanks a lot.
> Kapil
++imanshu.
------------------------------
Date: Sun, 4 Apr 2004 10:52:54 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Search and replace a string in files
Message-Id: <slrnc70bqm.m3a.tadmc@magna.augustmail.com>
Paul Lalli <ittyspam@yahoo.com> wrote:
> On Sun, 4 Apr 2004, Henry Law wrote:
>> On 3 Apr 2004 22:41:03 -0800, khoslakapil@yahoo.com (Kapil Khosla)
>> wrote:
>>
>> >Essentially I am just converting all the occurences of Main to main in
>> >a file.
>>
>> If you're doing this as an exercise then fine (and my Perl is nothing
>> like good enough to help you); but if you just need it done then maybe
>> some other tool would be better? I think a good sed-ist would do this
>> in one line.
>>
>
> So would a good Perl programmer. :-)
>
> perl -pi.bkp -e 's/Main/main/' file.txt
This is likely to be "better":
perl -pi.bkp -e 's/\bMain\b/main/g' file.txt
^^ ^^ ^
^^ ^^ ^
Or does the OP want "Maine" to become "maine"? ...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 4 Apr 2004 10:59:19 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Search and replace a string in files
Message-Id: <slrnc70c6n.m3a.tadmc@magna.augustmail.com>
Kapil Khosla <khoslakapil@yahoo.com> wrote:
> I am new to PERL
No, you are new to Perl.
There is no PERL that I'm aware of.
> Essentially I am just converting all the occurences of Main to main in
> a file.
>
> I read the chapter on Regular expressions
Excellent! Thank you for trying on your own before asking.
> Now, I dont know how to use this in a file and replace the string
> matched with the corresponding lower case main.
It kinda looks like you did not check the Perl FAQ before posting
to the Perl newsgroup though.
Please don't do that anymore.
> Can you help?
Your Question is Asked Frequently:
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?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 4 Apr 2004 10:07:43 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: test
Message-Id: <c4omn2$790$1@news.simnet.is>
"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x78yhci9oa.fsf@mail.sysarch.com...
> >>>>> "R" == Robin <webmaster@infusedlight.net> writes:
>
> R> "CatcherInTheRye" <letnomanenterhere@yahoo.co.kr> wrote in message
> R> news:c4g8b5$akp@netnews.proxy.lucent.com...
> >> test
> >>
> R> go away.
>
> take your own advice. you are not useful here.
this was not fair.
Robin may have faults, but was not out of line in this case.
Also, Robin is probably just an enthusiastic kid, just starting
programming, and may well develop some skills and wisdom with
time. There is no need to bully him or treat him as a pariah.
He will either come to realize that our criticisms of his code
has some value after all, or he will soose interest after
hitting too many walls. [how is it going with the BBS ? :-)]
If you cannot tolerate him, just plonk him.
gnari
------------------------------
Date: Sun, 04 Apr 2004 14:13:43 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: test
Message-Id: <x7n05rhlux.fsf@mail.sysarch.com>
>>>>> "TvP" == Tassilo v Parseval <tassilo.parseval@rwth-aachen.de> writes:
TvP> Also sprach Uri Guttman:
>>>>>>> "R" == Robin <webmaster@infusedlight.net> writes:
>>
R> "CatcherInTheRye" <letnomanenterhere@yahoo.co.kr> wrote in message
R> news:c4g8b5$akp@netnews.proxy.lucent.com...
>> >> test
>> >>
R> go away.
>>
>> take your own advice. you are not useful here.
TvP> Neither was this posting of yours. Uri, you do know the concept of
TvP> emails, don't you? They wouldn't make a comment like that less
TvP> superfluous, but please, write such things in private if you must.
i stand by it. robin posts bad code and claims it is good. then he has
the chutzpah to comment on other people's posts. a troll by any other
name. you seem to think he can actually learn. i would not agree to
this, given his attitude towards feedback. we have enough newbies here
who want to learn, that trolls like robin are just not worth helping
time on. remember the japanese moron monster (who shall remain unnamed
and has been gone for a nice while)? they need to be discouraged or they
will actually sell their crapware one day to some unsuspecting fool.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Sun, 4 Apr 2004 10:47:43 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: test
Message-Id: <slrnc70bgv.m3a.tadmc@magna.augustmail.com>
gnari <gnari@simnet.is> wrote:
> "Uri Guttman" <uri@stemsystems.com> wrote in message
> news:x78yhci9oa.fsf@mail.sysarch.com...
>> >>>>> "R" == Robin <webmaster@infusedlight.net> writes:
>>
>> R> "CatcherInTheRye" <letnomanenterhere@yahoo.co.kr> wrote in message
>> R> news:c4g8b5$akp@netnews.proxy.lucent.com...
>> >> test
>> >>
>> R> go away.
>>
>> take your own advice. you are not useful here.
>
> this was not fair.
>
> Robin may have faults, but was not out of line in this case.
^^^^^^^^^^^^
I agree that Uri was not fair in this case.
He is cross-pollenating different threads.
> Also, Robin is probably just an enthusiastic kid, just starting
> programming, and may well develop some skills and wisdom with
> time.
[
More that 3 years worth of time though?
Some of the advice he is still ignoring was given here back in 2000...
]
> There is no need to bully him or treat him as a pariah.
Right. In this case.
All kinds of nastiness would not be inappropriate in the case of
distributing hurtful code to the unsuspecting public.
(but that didn't happen in _this_ thread, so nastiness should not
be injected into this thread. Put the response-nastiness in the
same thread where the originating-nastiness occured, else readers
cannot learn by connecting the cause with the effect.
)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 04 Apr 2004 11:14:18 -0500
From: Chris Schadl <cschadl@hotmail.com>
Subject: Weird timezone problems on Win2k
Message-Id: <4070345A.E956917B@hotmail.com>
Consider the following snippet of code:
#!/usr/bin/perl
use strict;
use Date::Manip;
my $date = &ParseDate("today");
printf "Date is %s\n", UnixDate($date, "%u");
printf "Hour according to localtime() is %s\n", (localtime())[2];
On Linux:
chris@lain:~/src/perl$ cat /etc/timezone
America/Center
chris@lain:~/src/perl$ ./datetime.pl
Date is Sun Apr 4 11:14:55 -0500 2004
Hour according to localtime() is 11
However on Win2k, the time returned is 5 hours ahead:
E:\Perl>echo %TZ%
CDT
E:\Perl>perl datetime.pl
Date is Sun Apr 4 16:12:00 -0500 2004
Hour according to localtime() is 16
The system clock on the Win2k machine reports that the current time is
11:12am. Does anyone know wtf the problem is?
------------------------------
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 6362
***************************************