RE: Unix commands in Perl scripts
Tony, You simply enclose your unix commands in ''. i.e. #!untested 'ls -la' tim Timothy B Booher, Lt USAF Research Engineer AFRL/MNAC 101 W. Eglin Blvd. Eglin AFB, FL 32542-6810 Telephone: 850-882-8302 ext. 3360 Fax: 850-882-2201 E-Mail:[EMAIL PROTECTED] -Original Message- From: Ho, Tony [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 20, 2002 10:49 AM To: '[EMAIL PROTECTED]' Subject: Unix commands in Perl scripts Hi guys I was wondering if anyone knows how to execute Unix commands in a Perl Script ? I would be most grateful if you could let me know Thanks in advance Tony
windows perl just doesn't seem to work
Hello - I am very frustrated - after quite a while I can't get my ActivePerl to work on anything in windows. Say I want to find the word Good and change it to Bad in the file MyFile.asc I try: C:\changeIt.pl -e s/Good/Bad/ MyFile.asc Then open MyFile.asc and nothing happens - then I try this: # this is a script to clean open(IN,fileToClean.asc) || die can't open!; while(IN) { s/Good/Bad/; print; } On the screen I can see that this is working, but when I open the file, guess what? Nothing has changed! I tried a question on this same topic awhile ago and everybody said that they got it to work in cgywin and unix etc - I'll try again - can anybody figure out what I am doing wrong? Thanks, tim
can't print input argument
Hello - more trouble, I just can't seem to write a program that prints an argument it's passed: My script contains: #not working print $ARGV\n; when I run this I get: c:\work.pl this c:\ confused by this but also confused that I can't run anything from the command line in windows. Like c:\perl -e s/Bad/Good/ test.txt or c:\perl -e 's/Bad/Good/ test.txt I have activePerl installed and my file attributes are fine . . . tim
run from command line
Hello. I am trying to run a simple command to remove all the leading spaces from a file and it just isn't working. No errors, just no results - still a lot of leading spaces on each line. %perl -pi.bak -e 's/^\s+//' bomb1.txt any ideas? tim
difference between MSI and AS package
One more while I am at it . . . what is the difference between the MSI and the AS package distribution of ActivePerl 5.6.1 on the active state web-site? I don't know which one to download . . . Tim
RE: change all files in directory
When I try to run the one-liners I get: Can't open *.cpp: Invalid argument. Can't open *.hpp: Invalid argument. But when I do a dir command I get: target_functions.cpptarget_modules.cpp global_constants.hpp global_header.hpp class_functions.cpp S2b_4.opt Clearly these files are there . . . or am I just doing something silly . . . tim -Original Message- From: Brett W. McCoy [mailto:[EMAIL PROTECTED]] Sent: Friday, February 01, 2002 9:47 AM To: Booher Timothy B 1stLt AFRL/MNAC Cc: [EMAIL PROTECTED] Subject: Re: change all files in directory On Fri, 1 Feb 2002, Booher Timothy B 1stLt AFRL/MNAC wrote: Hello, I am trying to change all files in a directory that contain the strings Satellite satellite and SATELLITE that I need to change to target, Target and TARGET. Because many of these are C++ source files I need to preserve the case. I was thinking of the following script: #!/usr/bin/perl -w #UNTESTED @FilesInDirectory = *.cpp *.hpp *.asc; foreach $FileName (@FilesInDirectory) { open(IN, $FileName); whileIN { $_ =~ s/satellite/target/; $_ =~ s/Satellite/Target/; $_ =~ s/SATELLITE/TARGET/;} } but this just doesn't seem as efficient as it can be. I was trying to think of regex that could do it all in one line but it seemed so much simpler to do it in three. This can actually be done on the command-line: perl -pi.bak -e 's/satellite/target/g' *.cpp *.hpp. *.asc There's nothing wrong with doing it in three steps. -- Brett http://www.chapelperilous.net/ Trust me: Translation of the Latin caveat emptor.
Wade through a file and grab titles
I have a bunch of files in the form below and I want to go through the list and extract only the file name. The only way I can consistently see this is to take the next line after the ). ahmad73 __ exist: 0 K. Ahmad and I. J. Smalley, Powder Technol. 8, 69 (1973). Observation of particle segregation in vibrated granular systems . keywords: segregation vibration ahn91 __ exist: 0 H. Ahn, C. E. Brennen, and R. H. Sabersky, J. Appl. Mech. 58, 792 (1991). Observation of particle segregation in vibrated granular systems . ahn92 __ exist: 0 H. Ahn, C. E. Brennen, and R. H. Sabersky, J. Appl. Mech. 59, 109 (1992). Analysis of the fully developed chute flow of granular materials . I tried the following: # NOT WORKING open(LL,$ARGV[0]) || die can't open $ARGV for reading: $!; # input file open(LOGFILE,$ARGV[1]) || die can't open output file $ARGV[1]; #output file while(my $line = LL) { next if $line =~ /^\s*$/; if ($line =~ /\)\.\s*$/) { # attempt to match on .) at end of line $myTitle = ; print LOGFILE $myTitle; } } This is returning all files as if the regex were continually true . . . any body with a sharp eye
make a executable with a perl file in windows
Hello - I just loaded ActiveState perl onto my computer today . . . while I normally use perl on my unix machine and modify the file using chmod +x perlFile.pl - what can I do on dos or do I have to type perl PerlFile.pl everytime to run PerlFile? Thanks, Tim
one question to end the day on . . .
Hello out there - I have learned a lot of Perl today, but I am still trying to figure one more thing out. How can I go through a file and extract all the text between certain delimiters - for example I have: Bilbo, Why I like rings Freemont Press, 1998. Frodo, Why I don't Bridgedale Freemans, 1832 Etc I want to get: Why I like rings Why I don't It seems like there should be a real quick way to do this. . . thoughts? tim
text to html converter
Hello - when printing out a lot of my c++ code I go from a nice color syntaxed environment to a plain old black and white. If there isn't already something else out there I would like to write a Perl script that changes all comments to green for example and preserves the tab spacing into the html file. I am about to roll up my sleeves and start on this, but ya'll seem to know what is out there - does this exist already? Thanks, tim
RE: change the format of file.
Cool. I think I have an idea - what is your question? tim -Original Message- From: Anand, Pankaj [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 30, 2002 2:18 PM To: 'Beginners (E-mail) Subject: change the format of file. Hi , I want to make a perl script which can take the input from a file which is in this form - name1 name2 I want to change it to this format - definition1 name1 definition2 definition3 definition4:name1 definition1 name2 definition2 definition3 definition4:name1 and so on Thanx 952)324-0422 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Count Words
Wow -- that is really cool. I am going to go review hashes. How crazy compact! thanks a lot, Tim -Original Message- From: Peter Scott [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 22, 2002 9:40 AM To: Booher Timothy B 1stLt AFRL/MNAC; '[EMAIL PROTECTED]' Subject: Re: Count Words At 08:59 AM 1/22/02 -0600, Booher Timothy B 1stLt AFRL/MNAC wrote: I am trying to write a perl script to count the words (not counting duplicates) in a file based on the following definition of word: A word is any collection of characters seperated by white space or punctuation characters such as {.!?,} I have a lot of ideas, but also the suspicion that someone else has done this before. Here is my basic approach. -- create two-dimensional array with following axes {x = word.length, y = word.string} -- read line -- read first word -- compare word against entire column of similiar sized words if found then promote word one higher in column else add word to the bottom of the column and increment word count Any ideas on a more efficient approach -- anything else out there that does this? Whoa, sounds like someone hasn't met hashes yet. Hashes are the first coolest thing you encounter when learning Perl (unless you've come from awk, which I don't think you have). If we accept the set of word characters as being defined by \w, your problem can be solved with this code: my %word; while () { $word{$_}++ for /(\w+)/g; } Somewhat simpler than you were imagining? Here's how it works: my %word; Declare hash (since the code is going to run with use strict). while () { While we can read a line from either files named on the command line or standard input, put the line into the variable $_ for /(\w+)/g; Loop over all groups of consecutive word characters in $_, putting each one into a temporary $_ $word{$_}++ Increment the count stored in the hash corresponding to that word. If there isn't one there yet, create one with an initial value of 0, then add 1 to it. After the end of the loop you can dump the concordance with something like: print $_: $word{$_}\n for sort keys %word; -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com
take the file name in as an argument of the function
Hello all. I want to be able to take the file name in as an argument of the function, for example: %Extract.pl MyFile.txt I know that I could use: #!/usr/bin/perl while () { } But how could I take two arguments from the command line: i.e. my guess: %Test.pl Foo Bar #/!/usr/bin/perl $A = $ARGV[0]; $B = $ARGV[1]; print Thing A: $A Thing B: $B; Thanks, Tim
vCal (try2)
I sent this out last tuesday and no responses so far, just about 5 people emailing me to ask if I had gotten any answers which tells me there is a lot of interest in this question. If anyone has any knowledge in this subject matter, please do tell the rest of us as we are eager for an answer . . . thanks, tim p.s. several people asked about the vCal standard -- it can be found here: http://www.imc.org/pdi/ --- Sent: Tuesday, January 08, 2002 5:30 PM To: [EMAIL PROTECTED] Subject: Import Appointments into Outlook using Perl and vCalendar Hello all, At our lab we constantly have emails sent out in the form: 7-15 JanTesting of Product X (POC: Joe Smith) Room 265 2 Jan Leaders Meeting (POC: John Doe)Room 121 8 Jan Review (POC: Tom) Room 30 I am trying to take these word documents and create some time of vCalendar file so I can automatically import all of these dates into a personal information manager (such as Outlook or Palm). As it is everyone is laboriously adding these things to their respective calendars or manually inputting this information into Outlook. Does anyone know of a module that takes inputs and generates vCalendar files. Is there a better way to do this? Any thoughts? tim
Import Appointments into Outlook using Perl and vCalendar
Hello all, At our lab we constantly have emails sent out in the form: 7-15 JanTesting of Product X (POC: Joe Smith) Room 265 2 Jan Leaders Meeting (POC: John Doe)Room 121 8 Jan Review (POC: Tom) Room 30 I am trying to take these word documents and create some time of vCalendar file so I can automatically import all of these dates into a personal information manager (such as Outlook or Palm). As it is everyone is laboriously adding these things to their respective calendars or manually inputting this information into Outlook. Does anyone know of a module that takes inputs and generates vCalendar files. Is there a better way to do this? Any thoughts? tim
simple command line execution
Is it possible to use a simple perl script to do simple unix commands. (similar to the shell scripts)? For example I want to run a unix command on all files in a current directory: Shell script (just pick dos2unix for example): Foreach f (*.*) Dos2unix $f Echo $f . cleaned Done Thanks, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
is there ever a situation when you need a shell script instead of a perl script?
Thanks, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
remove spaces before and after . . . howdoi?
o.k. my program finally works (thanks for the help yesterday) . . . but I am convinced I am doing this the long way . . . I am sure there is a more elegant solution (prob a one-liner). Any thoughts . . . thanks, tim #!/usr/bin/perl -w # This program is just to test my ability to parse open(MYFILE,'test2.txt'); $lNum = 1; while (MYFILE){ $line = $_; chomp($line); next if $line =~ (/^\*+/)|(/^\s*$/); # no blank lines or lines that start with *** @splitLine = split(/:\s/,$line);# divide $splitLine[0] =~ s/^\s*//; # remove leading spaces from each one $splitLine[1] =~ s/^\s*//; $splitLine[0] =~ s/^\s+$//; # remove trailing spaces from each one $splitLine[1] =~ s/^\s+$//; print $lNum\: \$splitLine[0]\,\$splitLine[1]\\n; $lNum++; }; Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
RE: remove spaces before and after (thanks)
Thanks so much, but I am still confused on one point. I have: next if $line =~ (/^\*+/)|(/^\s*$/); and it seems to work, but you say: next if $line =~ /^(\*|\s*$)/; wouldn't this only find one * that the line starts with. i.e. * foo foo would both be skipped, what about $line =~ /^(\**|\s*$)/; ? I learned a lot from how you did the program, thanks again, tim
cycle through buffer
Why can't I do something like this? Load everything to a buffer then cycle through the buffer? Thanks, tim #!/usr/bin/perl -w open(MYFILE,'test2.txt'); while (MYFILE) { $buffer .= $_; } $lnum = 1; while ($buffer) { chomp; next if /^\*+/ or /^\s*$/; my ($field, $value) = split /\s*:\s+/; $field =~ s/^\s+//; # remove leading spaces from $field $value =~ s/\s+$//; # remove trailing spaces from $value print qq{$lnum: $field,$value\n}; $lnum++; }; Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
FW: question -- beginner's programmer's block (your answer)
I hope this serves as an answer . . . The original file is of this form: * foo *** thingA: 12 thingB: 23 thingC: 21 trial 1 colAcolBcolCcolD 1 23 28 273 227 2 237 232711 1 3 0 10 22 0 4 2 3 38 2 ..etc trial 2 === etc. this was the solution posed . . . -Original Message- From: Hanson, Robert [mailto:[EMAIL PROTECTED]] Sent: Monday, December 31, 2001 1:21 PM To: 'Booher Timothy B 1stLt AFRL/MNAC'; [EMAIL PROTECTED] Subject:RE: question-beginner's programmer's block I'm not sure that I completely understand, so this may or may not help. Anyway, I hope it does. If I understand correctly you want to grab each section and pass it to the appropriate sub for processing. There are lots of ways to do this, but this way came to mind first. It all works around a buffer and a state. When the *** line is seen the state is set to 1, when the line is seen the state is set to 2. When one of these is seen it executes the dispatch() sub to determine which sub should process the *LAST* sub seen. If the current line is not one of those section headers it buffers the line. So it will run something like this... * foo *** 1. run dispatch() sending the inital state of 0 (which will do nothing). set state to 1, go to next line. thingA: 12 2. buffer line thingB: 23 3. buffer line thingC: 21 4. buffer line trial 1 5. run dispatch() with a state of 1 and send the buffer (which passes the buffer to fooSub()). set state to 2, clear the buffer, and go to the next line. 6. buffer line colAcolBcolCcolD 7. buffer line 1 23 28 273 227 8 buffer line 2 237 232711 1 9. buffer line trial 2 === 10. run dispatch() with a state of 2 and send the buffer (which passes the buffer to trialSub()). set state to 2, clear the buffer, and go to the next line. etc, etc... The last dispatch() is called after the loop to process the last section to the end of file. # UNTESTED my $state = 0; my $buffer = ; while (my $line = MYFILE) { if ( $line =~ /^\*+/ ) { # this is the *** line dispatch($state, $buffer); # clear the buffer, set the new state # and go to the next line. $buffer = ; $state = 1; next; } elsif ( $line =~ /^=+/ ) { # this is a trail line dispatch($state, $buffer); # clear the buffer, set the new state # and go to the next line. $buffer = ; $state = 2; next; } # buffer the line $buffer .= $line; } # run the last section dispatch($state, $buffer); sub dispatch { my ($state, $buffer) = @_; # skip begining of file to first section return unless ( $state ); if ( $state == 1 ) { fooSub($buffer); } elsif ( $state == 2 ) { trialSub($buffer); } } sub fooSub { # do stuff} sub trialSub { # do stuff }
another easy question (actually 3)
o.k. here is another one: I still have a text file I am trying to get into a csv line, for example: cheese olives beans carrots So I put this into an array like: my @headers = split /\s+/; But now I want to print this in csv format: cheese,olives,beans,carrots. My best results have been to use: foreach (@headers) { print \$_\,; } But I am sure there is a better way, for example can I define the '$,' variable to be a comma? I have searched all my documentation and haven't come to any solid conclusion. Secondly, I find that I get a zero-length field for $headers[0] and don't understand why. Lastly, does /\s+/ mean 2 or more spaces or does it mean one or more spaces, I imagine that \s counts for one space and the '+' counts for one or more 'other' spaces - is this correct (i.e. I don't need /\s\s+/ to find two or more spaces). Laters, Tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
RE: another easy question (actually 3)
I can't get your implementation to work . . . What about s/^\s+|\s+$//g ? shouldn't it be s/(^\s+)|(\s+$)//g ? And why the if in: $_ = \$_\ if ( $_ =~ /[,]/ ); aren't you trying to keep that trailing comma off - I don't see where you add the comma. Tim __ you said . . . I would do something like this. ( I am not sure how efficient is this though ). open( INFILE, 'in.txt' ) or die; open( OUTFILE, 'out.txt' ) or die; while ( INFILE ) { s/^\s+|\s+$//g; # Eat leading trailing spaces next unless $_; # Skip empty lines my @fields = split /\s+/; foreach (@fields) { s///g; $_ = \$_\ if ( $_ =~ /[,]/ ); } print OUTFILE join( ',', @fields ), \n; } close INFILE; close OUTFILE;
find blank line
When parsing a text file - what is the best way to skip a blank line - when I find the length I get a length of 1 (perhaps the newline). I know that I could chomp($_) and then if(length($_) != 0) {do something} else {next} but I feel that there should be a way to have an if($_ = $blank) statement. Any thoughts . . . tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
RE: find blank line
Thanks so much - only one problem: when I try to use while (my $line = MYFILE) { etc } I get a bunch of errors similar to this one: Use of uninitialized value at JustForTest.pl line 13, MYFILE chunk 5. Do you know what I am doing wrong? Thanks, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201 -Original Message- From: Hanson, Robert [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 02, 2002 4:27 PM To: 'Booher Timothy B 1stLt AFRL/MNAC'; [EMAIL PROTECTED] Subject:RE: find blank line One way is to do this... while ( my $line = FILE ) { next if ( /^\s*$/ ); } Rob -Original Message- From: Booher Timothy B 1stLt AFRL/MNAC [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 02, 2002 5:19 PM To: [EMAIL PROTECTED] Subject: find blank line When parsing a text file - what is the best way to skip a blank line - when I find the length I get a length of 1 (perhaps the newline). I know that I could chomp($_) and then if(length($_) != 0) {do something} else {next} but I feel that there should be a way to have an if($_ = $blank) statement. Any thoughts . . . tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
RE: find blank line
First of all, why do I need to use defined(MYFILE) here is my code: #!/usr/bin/perl -w # open(MYFILE,'test2.txt'); $lNum = 1; while (my $line = MYFILE){ chomp($line); next if $line =~ ((/^\*+/) or (/^\s*$/)); @splitLine = split(/:\s/,$line); print $lNum\: \$splitLine[0]\ And \$splitLine[1]\\n; $lNum++; }; this doesn't work for me . . . thanks, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 02, 2002 4:54 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject:RE: find blank line That's right. But let me add my 5cents to it. If you decide to use the construct while ( MYFILE )... then it better be while( defined(MYFILE) ) { //... } [sathish] -Original Message- From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 02, 2002 2:44 PM To: Booher Timothy B 1stLt AFRL/MNAC Cc: Hanson, Robert; [EMAIL PROTECTED] Subject: RE: find blank line On Jan 2, Booher Timothy B 1stLt AFRL/MNAC said: Thanks so much - only one problem: when I try to use while (my $line = MYFILE) { etc } I get a bunch of errors similar to this one: Use of uninitialized value at JustForTest.pl line 13, MYFILE chunk 5. Do you know what I am doing wrong? The code given you has an error. You read the line into $line, but the regex is being tested on $_. Either do: while (MYFILE) { next if /^\s*$/; # ... } or while (my $line = MYFILE) { next if $line =~ /^\s*$/; # ... } -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: find blank line
Why is ((/^\*+/) or (/^\s*$/)) improper? It breaks down to: ( (/^\*+/) or (/^\s*$/) ) How is this different than: next if $line =~ /^\*+/ or $line =~ /^\s*$/; except that it is less ambiguous? tim
question -- beginner's programmer's block
Hello - I am trying to figure out how to convert an output file to a comma separated values file. The original file is of this form: * foo *** thingA: 12 thingB: 23 thingC: 21 trial 1 = colAcolBcolCcolD 1 23 28 273 227 2 237 232711 1 3 0 10 22 0 4 2 3 38 2 .etc trial 2 etc. My dilemma so basic - I know that when I open this file as MYFILE I can cycle through this program as While MYFILE but when I do this I have to cycle through everything at once and have a bunch of conditional statements. To get around this, my idea is to have a main loop with two subroutines: say FooSub and TrialSub. For the first I would split on ':' for the next I would split on '\s+'. But how do I skip a line? I want to get past the . . . line but if I use next to start the subroutine won't that create a loop that reads no lines - perhaps an ((if $_ !~ /^\*{1,5}/) {skip} would be best and just have each module end when it hits an '='. Can someone give me basic advice if I am going about this in the correct direction? I feel like I need a little nudge in the right direction before I try to make a potentially broken idea work. Thanks, Tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
quick regex question
o.k. another regex issue . . . I want a one-liner that can remove everything after a given character: i.e. in this case everything after ! (fortran comment): would this work: perl -npe 's/\!.+$//' my thinking is that \! Is the literal character and . would count for anything + would represent any number of anything and $ would symbolize the end of the line. Am I close, if so how close? Thanks so much, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
RE: quick regex question
Hello and thanks so much for your replies, but don't I need a \! To denote a literal ! and what about spaces before the bang - don't I want to lose them too - what about: perl -pi.bak -e 's/\s\!.*$//' would that get rid of the spaces too? tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201 -Original Message- From: Curtis Poe [mailto:[EMAIL PROTECTED]] Sent: Wednesday, December 12, 2001 2:22 PM To: Booher Timothy B 1stLt AFRL/MNAC; [EMAIL PROTECTED] Subject:Re: quick regex question --- Booher Timothy B 1stLt AFRL/MNAC [EMAIL PROTECTED] wrote: o.k. another regex issue . . . I want a one-liner that can remove everything after a given character: i.e. in this case everything after ! (fortran comment): would this work: perl -npe 's/\!.+$//' my thinking is that \! Is the literal character and . would count for anything + would represent any number of anything and $ would symbolize the end of the line. Am I close, if so how close? Thanks so much, tim Ugh. In my previous example, I had non-capturing parens and made it optional. I had them because this was a slightly different regex, originally. What follows is better (with the caveat that any exclamation points that don't mark quotes are going to cause problems. perl -pi.bak -e 's/!.*$//' list of files Cheers, Curtis Ovid Poe = Senior Programmer Onsite! Technology (http://www.onsitetech.com/) Ovid on http://www.perlmonks.org/ __ Do You Yahoo!? Check out Yahoo! Shopping and Yahoo! Auctions for all of your unique holiday gifts! Buy at http://shopping.yahoo.com or bid at http://auctions.yahoo.com
RE: Use perl to change File names
Over the last evening I have reviewed these scripts and understand what is going on, but they still don't quite solve my problem. I want to fix the following situation. Image4.jpg, image5.jpg, image6.jpg - to -- foo78.jpg, foo79.jpg, foo80, etc .. .. This means I want to change the number not just the word involved. So for example I would want to give the parameter StartAt: '78' and 's/image/foo' - say I wanted to start at any arbitrary number given another arbitrary number, by specifing the offset (here 74). This is what I am having trouble doing . . . I have a feeling that it is possible . . . or did the original solution peter gave me provide a slight modification . . . ? Thanks for any help . . . tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201 -Original Message- From: Peter Scott [mailto:[EMAIL PROTECTED]] Sent: Monday, December 10, 2001 12:09 PM To: Booher Timothy B 1stLt AFRL/MNAC; [EMAIL PROTECTED] Subject:Re: Use perl to change File names At 11:28 AM 12/10/01 -0600, Booher Timothy B 1stLt AFRL/MNAC wrote: I want to be able to rename a file name from one number series to another number series ie: Image1.jpg Image2.jpg Image3.jpg to Foo234.jpg Foo235.jpg Foo236.jpg Is there a way to do this using regular expressions? Is there a more elegant way to adding a counter to the script above and hard-code the argument? Quick-n-dirty: for (glob(Image*.jpg)) { /(\d+)\./ and rename $_, Foo$1.jpg; } A bit more flexible: # my_rename(Image, Foo); sub my_rename { my ($from, $to) = @_; for (glob($from*.jpg)) { /(\d+)\./ and rename $_, $to$1.jpg; } } Even more so: # my_rename('Image(\d+).jpg', 'Foo$1.jpg'); sub my_rename { my $from = qr/^$_[0]$/s; for (glob *) { next unless /$from/; my $to = eval qq($_[1]); rename $_, $to unless -e $to; } } Watch that the backslashes get passed through properly, and be aware that glob() can have trouble with large result sets pre-5.6. -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Use perl to change File names
Hello - I have been trying to use perl to change filenames this morning, and have been having a lot of luck, but have hit a stopping point. I have lwall's code: #!/usr/local/bin/perl # # Usage: rename perlexpr [files] ($regexp = shift @ARGV) || die Usage: rename perlexpr [filenames]\n; if (!@ARGV) { @ARGV = STDIN; chomp(@ARGV);} foreach $_ (@ARGV) { $old_name = $_; eval $regexp; die $@ if $@; rename($old_name, $_) unless $old_name eq $_; } exit(0); but I want to be able to rename a file name from one number series to another number series ie: Image1.jpg Image2.jpg Image3.jpg to Foo234.jpg Foo235.jpg Foo236.jpg Is there a way to do this using regular expressions? Is there a more elegant way to adding a counter to the script above and hard-code the argument? Thanks for any help, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201 -Original Message- From: COLLINEAU Franck FTRD/DMI/TAM [mailto:[EMAIL PROTECTED]] Sent: Monday, December 10, 2001 4:08 AM To: '[EMAIL PROTECTED]' Subject:Stat function Greetings, I have the following code: opendir (INFO,l:/01_informatique) || die impossible d'ouvrire le répertoire 01_informatique: $!; while ($fic=readdir INFO) { if($fic=~/^c/) { @date=stat($fic); print $date[7]\n; } } close INFO; There is nothing in the screen ! Best regards, Franck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
thanks for the info on removing control m's
Thanks a lot - I have set up an alias to make my life easier. I am wondering how you would use the VB equivalents of Left$ and Right$ with perl. For example I have 'ABCDEFGH' and I want to get the right three letters: Right('abcdefgh',3) = 'fgh' Left can be done with substring (x,0) I am sure, but what about right? Thanks again, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
SIMPLE SCRIPT TO REMOVE ALL SPACE FROM THE ENDS OF LINES
I am looking for a simple way to go through my text file and remove all spaces from the ends of lines. I have a feeling that this is real easy, but don't know what function is best: does chomp do this? Is there a feature in emacs that does this better? Thanks for any information . . . Tim, brand New perl user Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201
perl script to remove control M's
I have what seems to be a persistent problem with all files that I import from dos into emacs. I have those control M's all over the place. Does anyone know of a perl script to 'clean' these files? Thanks so much, tim Timothy B Booher, Lt USAF, AFRL/MNAC 101 West Eglin Blvd, Suite 339 Eglin AFB FL 32542-6810 Phone: 850-882-8302 Ext. 3360 (DSN 872-) FAX: 850-882-2201