Re: Removing file extension
On Jan 23, 2007, at 8:22 AM, Saravana Kumar wrote: Hi list, I am trying to remove the extension from the a list of filenames and manipulate the names further. Tried to doing this: $file=~ s/\..*//; The above works fine. I get the result 'filename' if the filename is filename.ext. There are some files whose names are like file.name.ext and the result i get for this is 'file' while the desired result is 'file.name'. Is there a way to fix this? Yes, you need to assert in the regexp dots are not allowed in the extension, for example something like this: $file =~ s/\.\w+$//; Note that no captures are needed. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Removing file extension
Saravana Kumar wrote: shaick mohamed wrote: On 1/23/07, Saravana Kumar [EMAIL PROTECTED] wrote: I am trying to remove the extension from the a list of filenames and manipulate the names further. Tried to doing this: $file=~ s/\..*//; The above works fine. I get the result 'filename' if the filename is filename.ext. There are some files whose names are like file.name.ext and the result i get for this is 'file' while the desired result is 'file.name'. Is there a way to fix this? Try this s/(.*)\..*/\1/; I got this warning: \1 better written as $1 But the result was what i expected(file.name) (Please bottom-post all responses. Thank you.) $file =~ s/(.*)\./$1/; or $file =~ s/\.[^.]*$//; Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Removing file extension
On 2007/01/23, at 11:03, Rob Dixon wrote: $file =~ s/(.*)\./$1/; or $file =~ s/\.[^.]*$//; If you know the suffix of the files you're working on, you can use the File::Basename module, more specific the fileparse function: use File::Basename; my @suffix = qw(.txt .zip .doc); my $filepath = /tmp/something.txt; my ($name, $path, $suffix) = fileparse($filepath, @suffix); HTH! -- Igor Sutton [EMAIL PROTECTED] PGP.sig Description: This is a digitally signed message part
[JOB] Web Developers for publishing company - Charlotte, NC
See our posting at http://jobs.perl.org/job/5243 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to customize Perl installation
On 1/22/07, Jeff Peng [EMAIL PROTECTED] wrote: I just need Perl core and CGI.pm to be installed on my host.How can I do it?thanks. I'm using RedHat Linux The standard installation instructions should work for you. Look for the file called INSTALL in the source distribution. Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to customize Perl installation
On Mon, 2007-01-22 at 20:09 -0800, Jeff Peng wrote: It depends on what operating system you are using on your host. HI, I'm using RedHat Linux (AS4) of 2.6 kernel.Thanks. I'm not experienced with Red Hat, but I'd bet it already contains Perl core and CGI.pm. Otherwise, find CGI at http://search.cpan.org/~lds/CGI.pm-3.25/CGI.pm Cheers, David. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
Here is a snippet of the code: my $MyFileHand; my $MyFileHand1; open($MyFileHand,$MyFileIn) || diet (3, $MyFileIn, $!); open($MyFileHand1,$MyFileIn1) || diet (3, $MyFileIn1, $!); proc_getrcd( $MyFileHand , $MyEOFProd, $MyWorkp, $MyInp , $GlblInfo{compcnt}, $MyUnpackSw ); proc_getrcd( $MyFileHand1, $MyEOFTest, $MyWorkt, $MyInt , $GlblInfo{compcnt}, $MyUnpackSw ); Subroutine being called: sub proc_getrcd { #0 1 2 3 4 5 my ( $MyFileHand, $MyEOF, $MyWork, $MyIn, $MyRunningCnt, $MyUnpack ) = @_; return if ( $MyEOF ); my $MyData = $MyFileHand; if ( ! defined $MyData ) { $_[1] = 1; printf EOF hit on read: %7d\n, $MyIn; return; } $_[3]++; $_[4]++; chomp($MyData); $MyData =~ s/[[:cntrl:]]/ /g if ( $MyData =~ /[[:cntrl:]]/ ); if ( $MyUnpack ) { @{$MyWork} = unpack($GlblInfo{unpackv},$MyData); } else { $MyWork-[0] = substr($MyData,10,16); $MyWork-[1] = substr($MyData,1126,10); $MyWork-[0] =~ s/\s+//g; } } # end of proc_getrcd I have three main subs that I am using to print and then compare the data: sub 1 reads the first 10 rcds of the Test file and prints out the data sub 2 reads the first 10 rcds of the Prod file and prints out the data Now these subs use the the same above sub, but is doing the unpack into the passed array. I have checked the output in the different elements and they are different in value and the key for each record is there so I know it has gottent the next rcd. sub 4 is the compare and reading of both files, but always ends up only doing the read Know I am mssing something very basic, but escapes me. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. **
Re: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
On 1/23/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: $MyData =~ s/[[:cntrl:]]/ /g if ( $MyData =~ /[[:cntrl:]]/ ); Why the if clause? I'm not sure I understand your difficulty. But it sounds as if you're not using seek() (or something similar) to get back to the correct data position in your file, when you call your subroutine the second time. If that's not it, can you identify which line of code isn't doing what you think it should? Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
I am just trying to read text files which are delimited by a regular end of line. I usually only read one file at a time, but thought it should not be that big a thing to have two file handles open and pass the filehandle to the sub. Can I not ready two different text files at the same time using two different filehandles? If you have any problems or questions, please let me know. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tom Phoenix Sent: Tuesday, January 23, 2007 11:51 To: Wagner, David --- Senior Programmer Analyst --- WGO Cc: Beginner Perl Subject: Re: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF On 1/23/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: $MyData =~ s/[[:cntrl:]]/ /g if ( $MyData =~ /[[:cntrl:]]/ ); Why the if clause? I'm not sure I understand your difficulty. But it sounds as if you're not using seek() (or something similar) to get back to the correct data position in your file, when you call your subroutine the second time. If that's not it, can you identify which line of code isn't doing what you think it should? Hope this helps! --Tom Phoenix Stonehenge Perl Training ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
On 1/23/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: I am just trying to read text files which are delimited by a regular end of line. I usually only read one file at a time, but thought it should not be that big a thing to have two file handles open and pass the filehandle to the sub. Can I not ready two different text files at the same time using two different filehandles? What you describe should be possible. How are you relocating the file position to the correct place in the file, when you wish to re-read the data after the first time? Does the subroutine use seek(), or is it the caller's responsibility? --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
It is two different files. Sub1 and sub2 read from 1 file for 10 rcds and then closes the files and goes on to the next sub? If I have two different filehandles pointing at two different files, why would I have to do a seek? I am just trying to read two text files at the same time and determine if key fields are equal or not. If not, then determine the lower of the two, add the numeric field, read the next rcd from this file and go back to top of the loop again and start the compare process over. If you have any problems or questions, please let me know. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tom Phoenix Sent: Tuesday, January 23, 2007 12:03 To: Wagner, David --- Senior Programmer Analyst --- WGO Cc: Beginner Perl Subject: Re: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF On 1/23/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: I am just trying to read text files which are delimited by a regular end of line. I usually only read one file at a time, but thought it should not be that big a thing to have two file handles open and pass the filehandle to the sub. Can I not ready two different text files at the same time using two different filehandles? What you describe should be possible. How are you relocating the file position to the correct place in the file, when you wish to re-read the data after the first time? Does the subroutine use seek(), or is it the caller's responsibility? --Tom Phoenix Stonehenge Perl Training ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF
Sorry, but it was a logic problem and onthing else. I apologize for missing it, but I did. If you have any problems or questions, please let me know. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us -Original Message- From: Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 23, 2007 12:13 To: Tom Phoenix Cc: Beginner Perl Subject: RE: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF It is two different files. Sub1 and sub2 read from 1 file for 10 rcds and then closes the files and goes on to the next sub? If I have two different filehandles pointing at two different files, why would I have to do a seek? I am just trying to read two text files at the same time and determine if key fields are equal or not. If not, then determine the lower of the two, add the numeric field, read the next rcd from this file and go back to top of the loop again and start the compare process over. If you have any problems or questions, please let me know. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tom Phoenix Sent: Tuesday, January 23, 2007 12:03 To: Wagner, David --- Senior Programmer Analyst --- WGO Cc: Beginner Perl Subject: Re: Trying to read two files using a sub to get the data, but 2nd read on the 2nd file goes to EOF On 1/23/07, Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] wrote: I am just trying to read text files which are delimited by a regular end of line. I usually only read one file at a time, but thought it should not be that big a thing to have two file handles open and pass the filehandle to the sub. Can I not ready two different text files at the same time using two different filehandles? What you describe should be possible. How are you relocating the file position to the correct place in the file, when you wish to re-read the data after the first time? Does the subroutine use seek(), or is it the caller's responsibility? --Tom Phoenix Stonehenge Perl Training ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Removing file extension
Igor Sutton Lopes wrote: On 2007/01/23, at 11:03, Rob Dixon wrote: $file =~ s/(.*)\./$1/; or $file =~ s/\.[^.]*$//; If you know the suffix of the files you're working on, you can use the File::Basename module, more specific the fileparse function: use File::Basename; my @suffix = qw(.txt .zip .doc); my $filepath = /tmp/something.txt; my ($name, $path, $suffix) = fileparse($filepath, @suffix); You don't need to know the suffix name(s), just use a regular expression: my $filepath = '/tmp/something.txt'; my ( $name, $path, $suffix ) = fileparse( $filepath, qr/\.[^.]*/ ); 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: How to customize Perl installation
I'm not experienced with Red Hat, but I'd bet it already contains Perl core and CGI.pm. Sorry,I mean I only need Perl core and CGI.pm to be installed on my host,other modules are excluded. Cheap talk? Check out Yahoo! Messenger's low PC-to-Phone call rates. http://voice.yahoo.com Now that's room service! Choose from over 150,000 hotels in 45,000 destinations on Yahoo! Travel to find your fit. http://farechase.yahoo.com/promo-generic-14795097 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
trouble with list context assignment for substitution inside File::Find wanted function
Hi, I have a directory which contains several files. client1-2006-05-19.log.gz client1-2006-05-20.log.gz client1-2006-07-29.log.gz client1-2006-10-05.log.gz client1-2006-05-21.log.gz I want strip all of axisglobal- in their filenames. What I did was: #!/usr/bin/perl use warnings; use strict; use File::Find; find (\renamefiles, './'); sub renamefiles{ (my $newname) = $_ =~ s/^\w+-//g; #rename ($_, $newname); print $newname; } When I try printing the $newname which supposedly will print only 2006-N-N.log.gz, it instead prints a scalar value of 1, as if parenthesis around my $newname does not exists. And so, uncommenting the rename did do anything to my files. Any explanation to this? Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+... thanks. Thanks. Do you Yahoo!? Everyone is raving about the all-new Yahoo! Mail beta. http://new.mail.yahoo.com
Re: trouble with list context assignment for substitution inside File::Find wanted function
Michael Alipio wrote: Hi, Hello, I have a directory which contains several files. client1-2006-05-19.log.gz client1-2006-05-20.log.gz client1-2006-07-29.log.gz client1-2006-10-05.log.gz client1-2006-05-21.log.gz I want strip all of axisglobal- in their filenames. What I did was: #!/usr/bin/perl use warnings; use strict; use File::Find; find (\renamefiles, './'); sub renamefiles{ (my $newname) = $_ =~ s/^\w+-//g; #rename ($_, $newname); print $newname; } When I try printing the $newname which supposedly will print only 2006-N-N.log.gz, it instead prints a scalar value of 1, as if parenthesis around my $newname does not exists. And so, uncommenting the rename did do anything to my files. Any explanation to this? Yes, the substitution operator (s///) returns true (1) or false ('') in either list or scalar context. To do want you want you have to do the assignment first and then do the substitution: my $newname = $_; $newname =~ s/^\w+-//; Or in one statement: ( my $newname = $_ ) =~ s/^\w+-//; Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+. No. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: trouble with list context assignment for substitution inside File::Find wanted function
- Original Message From: John W. Krahn [EMAIL PROTECTED] To: Perl Beginners beginners@perl.org Sent: Wednesday, January 24, 2007 10:57:51 AM Subject: Re: trouble with list context assignment for substitution inside File::Find wanted function Yes, the substitution operator (s///) returns true (1) or false ('') in either list or scalar context. To do want you want you have to do the assignment first and then do the substitution: my $newname = $_; $newname =~ s/^\w+-//; Or in one statement: ( my $newname = $_ ) =~ s/^\w+-//; I've already figured that one out. However, I want to use variables for my regexp pattern. So I can replace axis with whatever I my first program argument is. Changed this:1 find (\renamefiles, './'); sub renamefiles{ if ($_ =~ /axis/){ my $oldname = $_; $_ =~ s/\w+-//; #rename ($oldname, $_) print $oldname will be renamed to $_\n; } } To this: find (\renamefiles, './'); my $name = shift; sub renamefiles{ if ($_ =~ /$name/){ my $oldname = $_; $_ =~ s/\w+-//; #rename ($oldname, $_) print $oldname will be renamed to $_\n; } } And if I do a #perl rename.pl axis I got many of this: Use of uninitialized value in regexp compilation at test.pl line 11. Even if I specify axis in my $name instead of shift, I'm getting the same error What's wrong with using variables in regexp patterns?? 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ Yahoo! Music Unlimited Access over 1 million songs. http://music.yahoo.com/unlimited
Re: trouble with list context assignment for substitution inside File::Find wanted function
Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+. No. Yes. /^\w+-/ and rename $_, $' for (glob *) -Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
getopt
I found an example using getopt on the web and I am trying to convert it to my use. Everything works except the last part. What am attempting to do is create a script which I can pass switches as arguments. Eventually this script will replace the rm command on my linux server, so that I can create a trashcan. The end process will be myscript.pl -rf /home/myfolder and that will pass the -rf and /home/myfolder arguments to the rm command after copying everything to the trashcan. The script as is gets an error when run. My problem is how to make the argument '-rf' pass to the system() command then I plan to use ARGV[0] to pass the directory. #!/usr/bin/perl use warnings; use strict; use vars qw/ %opt /; sub init() { use Getopt::Std; my $opt_string = 'adfirv'; getopts( $opt_string, \%opt ) or usage(); usage() if $opt{h}; } sub usage() { print STDERR EOF; This program does... usage: $0 [-adfirv] [-f file] bla bla bla example: $0 -adfirv file EOF exit; } init(); my @option; my $options; push @option = a if $opt{a}; push @option = d if $opt{d}; push @option = f if $opt{f}; push @option = i if $opt{i}; push @option = r if $opt{r}; push @option = v if $opt{v}; $options = `print @option`; system(ls -$options); print \n;
Re: Removing file extension
Saravana Kumar schreef: I am trying to remove the extension from the a list of filenames and manipulate the names further. Tried to doing this: $file=~ s/\..*//; The above works fine. I get the result 'filename' if the filename is filename.ext. There are some files whose names are like file.name.ext and the result i get for this is 'file' while the desired result is 'file.name'. Is there a way to fix this? TIA, SK Use an anchor and a negated character set: s/\.[^.]*$// -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/