RE: question about grep
Eric, > -Original Message- > From: Hawley, Eric [mailto:[EMAIL PROTECTED]] > actually I think I see what is wrong with the code. At > first I was reading the wrong variable which was producing > just the word "Status" and second after looking at the grep > function again it looks as though I will have to store the > file into a data structure and grep from that data structure > to get the desired results. There is, however, a really nice feature of the magical diamond "<>" that can help you. If you're writing one-shot code it can be a real time saver, but should not be used in production code as it's mostly silent when things go wrong. # pretend we got a whole load of lines from the command line @ARGV=<*.txt>; # grep the magical <> my @results = grep /^Status/, <>; As if by magic... Alistair --- Registered Office: Marks & Spencer p.l.c Michael House, Baker Street, London, W1U 8EP Registered No. 214436 in England and Wales. Telephone (020) 7935 4422 Facsimile (020) 7487 2670 www.marksandspencer.com Please note that electronic mail may be monitored. This e-mail is confidential. If you received it by mistake, please let us know and then delete it from your system; you should not copy, disclose, or distribute its contents to anyone nor act in reliance on this e-mail, as this is prohibited and may be unlawful. The registered office of Marks and Spencer Financial Services PLC, Marks and Spencer Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and Marks and Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
I'm gonna de-lurk for this one. If you look at Peter's example, you will note that grep applies a block-or-something to a list and returns a list. You are applying it to the file name, not the contents. Perl grep doesn't read files. If this wasn't the win32 perl list, I would just advise that you really want the shell's grep [ like $lines = `grep "^Status" $project_file` to get the Status line(s) from one file ] Strictly within perl, you would need to open the file and snarf in its contents to grep. Maybe it would look like this (off the cuff, if errors, please point them out, to add value for readers): open (IFILE, "<$project_file" ) or die "useful message here:$!"; @lines = grep /^Status/, ; close (IFILE); # At this point, @lines has all the lines from $project_file that start with Status. # If these files are HUGE, it might be better to roll your own match loop with while () {...} HTH, Joe Dial -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 1:49 PM To: 'Peter Guzis'; Perl-Win32 (E-mail) Subject: RE: question about grep I tried what you did suggested and it only pulled out the word Status, here is the code below; #location contains the path for the report to be stored $report_location = $location; $report_location =~ s/\//\\/; $report_location = "$report_location" . "\\Reports"; unless( -e $report_location) ){ mkdir( $report_location ) or die "died creating Report Directory"; } #open Report HTML file open( FILE, "> $report_location\\$year_$month_$date_Report.htm" ) or die "Dead creating Report"; #print intial html to report file print FILE "\n\n$year_$month_$date_report\n\n\n "; #file paths include paths to the files, that Status should be greped from #ex) C:\\Test.htm foreach $project_file ( @file_paths ){ #grep Status line from $project_file @status_line = grep /^Status/, $project_file; } print FILE "\n\n"; close(FILE); Hope this can point to any failures. Thanks Eric -- -Original Message----- From: Peter Guzis [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 1:33 PM To: Perl-Win32 (E-mail) Subject: RE: question about grep @matches = grep /^Status/, @data; If this doesn't do it you might consider posting some sample data. Peter Guzis Web Administrator, Sr. ENCAD, Inc. - A Kodak Company email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 10:23 AM To: Perl-Win32 (E-mail) Subject: question about grep I got a question concerning grep. I would like to use it to pull out all full lines of text, that starts with the word "Status", from a list of files. I am not really too experienced with using Metacharacters and Metasymbols and do not know how to go about doing this with grep. Can someone help me out with this? Thanks in advanced Eric ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
Eric I'm in a pretty-code-mode at the moment, my apologies for the overkill. > #location contains the path for the report to be stored > $report_location = $location; > $report_location =~ s/\//\\/; To make this regex look a bit better you can use a different delimiter rather than "/". Also you probably want to replace all "\" chars with "/" in which case you need a 'g' at the end to make it a global replacement. Perl has a much faster construct for replacing one character with another, i.e. "tr". $report_location =~ s!\/!\\!g; # better $report_location =~ tr!\\!//!; # best; See "perlop" for more info on "s" and "tr" > $report_location = "$report_location" . "\\Reports"; A more explicit way of saying this using the ".=" construct. It saves you having to double check that your left hand side is exactly the same as you right hand side. It seems trivial here but is a godsend when debugging something like $x[10101]{$y[101]]}= $x[10101]{$y[101]]} + $y[101]; $report_location.="\\Reports" > unless( -e $report_location) ){ > mkdir( $report_location ) or die "died creating Report Directory"; > } The -e operator tells you that a file exists. We have inherited from Unix that a directory is a file. You might also want to check that $report_location exists and is a directory. Id also suggest being a little more informative to your users. Tell them what went wrong with the $! variable. Also the idiom _this_ or _that_ can be IMHO elegantly extended here -d $report_location or mkdir $report_location or die "died creating Report Directory '$report_location': $!"; > #open Report HTML file > open( FILE, "> > $report_location\\$year_$month_$date_Report.htm" ) or die > "Dead creating Report"; Note that perl will interpret this as "$year_"."$month_" etc. You probably called your variables $year $month etc. So, to force perl to recognise where the end of you variables are use {}'s. Make sure you put "use strict;" at the top of your code and "my" all your variables otherwise perl will silently evaluate them to "". my $report_name=$report_location\\${year}_${month}_${date}_Report.htm"; open FILE, ">$report_name") or die "Cannot write to $report_name : $!"; > #print intial html to report file > print FILE > "\n\n$year_$month_$date_report\n ead>\n\n > "; A more elegant construct for printing long strings is the "< $year_$month_$date_report Etc, END_OF_HTML_HEADER > foreach $project_file ( @file_paths ){ > #grep Status line from $project_file > @status_line = grep /^Status/, $project_file; > } Note, $project_file will be a file name, not the contents of that file. You need to open it and read : foreach my $project_file ( @file_paths ){ open PROJFILE, $project_file or die "Cannot read $project_file : $!"; my @status_lines = grep /^Status/, ; # Using the file in an array context; close PROJFILE; DoStuff(@status_lines); } HTH Alistair --- Registered Office: Marks & Spencer p.l.c Michael House, Baker Street, London, W1U 8EP Registered No. 214436 in England and Wales. Telephone (020) 7935 4422 Facsimile (020) 7487 2670 www.marksandspencer.com Please note that electronic mail may be monitored. This e-mail is confidential. If you received it by mistake, please let us know and then delete it from your system; you should not copy, disclose, or distribute its contents to anyone nor act in reliance on this e-mail, as this is prohibited and may be unlawful. The registered office of Marks and Spencer Financial Services PLC, Marks and Spencer Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and Marks and Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
actually I think I see what is wrong with the code. At first I was reading the wrong variable which was producing just the word "Status" and second after looking at the grep function again it looks as though I will have to store the file into a data structure and grep from that data structure to get the desired results. Thanks for your input Eric -Original Message- From: Peter Guzis To: Perl-Win32 (E-mail) Sent: 2/13/03 1:32 PM Subject: RE: question about grep @matches = grep /^Status/, @data; If this doesn't do it you might consider posting some sample data. Peter Guzis Web Administrator, Sr. ENCAD, Inc. - A Kodak Company email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 10:23 AM To: Perl-Win32 (E-mail) Subject: question about grep I got a question concerning grep. I would like to use it to pull out all full lines of text, that starts with the word "Status", from a list of files. I am not really too experienced with using Metacharacters and Metasymbols and do not know how to go about doing this with grep. Can someone help me out with this? Thanks in advanced Eric ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
Do you want to pull out lines FROM files, or from a list of files? e.g.: to print a list of lines beginning with 'my' from a directory of perl scripts: while (<*>){ open IN, "$_"; @matches = grep {/^my/} ; close IN; } print @matches HTH From: "Hawley, Eric" <[EMAIL PROTECTED]> To: "'Peter Guzis'" <[EMAIL PROTECTED]>, "Perl-Win32 \(E-mail\)" <[EMAIL PROTECTED]> Subject: RE: question about grep Date: Thu, 13 Feb 2003 13:49:20 -0500 I tried what you did suggested and it only pulled out the word Status, here is the code below; #location contains the path for the report to be stored $report_location = $location; $report_location =~ s/\//\\/; $report_location = "$report_location" . "\\Reports"; unless( -e $report_location) ){ mkdir( $report_location ) or die "died creating Report Directory"; } #open Report HTML file open( FILE, "> $report_location\\$year_$month_$date_Report.htm" ) or die "Dead creating Report"; #print intial html to report file print FILE "\n\n$year_$month_$date_report\n\n\n "; #file paths include paths to the files, that Status should be greped from #ex) C:\\Test.htm foreach $project_file ( @file_paths ){ #grep Status line from $project_file @status_line = grep /^Status/, $project_file; } print FILE "\n\n"; close(FILE); Hope this can point to any failures. Thanks Eric ------ -----Original Message- From: Peter Guzis [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 1:33 PM To: Perl-Win32 (E-mail) Subject: RE: question about grep @matches = grep /^Status/, @data; If this doesn't do it you might consider posting some sample data. Peter Guzis Web Administrator, Sr. ENCAD, Inc. - A Kodak Company email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 10:23 AM To: Perl-Win32 (E-mail) Subject: question about grep I got a question concerning grep. I would like to use it to pull out all full lines of text, that starts with the word "Status", from a list of files. I am not really too experienced with using Metacharacters and Metasymbols and do not know how to go about doing this with grep. Can someone help me out with this? Thanks in advanced Eric ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs _ STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
I tried what you did suggested and it only pulled out the word Status, here is the code below; #location contains the path for the report to be stored $report_location = $location; $report_location =~ s/\//\\/; $report_location = "$report_location" . "\\Reports"; unless( -e $report_location) ){ mkdir( $report_location ) or die "died creating Report Directory"; } #open Report HTML file open( FILE, "> $report_location\\$year_$month_$date_Report.htm" ) or die "Dead creating Report"; #print intial html to report file print FILE "\n\n$year_$month_$date_report\n\n\n "; #file paths include paths to the files, that Status should be greped from #ex) C:\\Test.htm foreach $project_file ( @file_paths ){ #grep Status line from $project_file @status_line = grep /^Status/, $project_file; } print FILE "\n\n"; close(FILE); Hope this can point to any failures. Thanks Eric -- -Original Message- From: Peter Guzis [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 1:33 PM To: Perl-Win32 (E-mail) Subject: RE: question about grep @matches = grep /^Status/, @data; If this doesn't do it you might consider posting some sample data. Peter Guzis Web Administrator, Sr. ENCAD, Inc. - A Kodak Company email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 10:23 AM To: Perl-Win32 (E-mail) Subject: question about grep I got a question concerning grep. I would like to use it to pull out all full lines of text, that starts with the word "Status", from a list of files. I am not really too experienced with using Metacharacters and Metasymbols and do not know how to go about doing this with grep. Can someone help me out with this? Thanks in advanced Eric ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: question about grep
@matches = grep /^Status/, @data; If this doesn't do it you might consider posting some sample data. Peter Guzis Web Administrator, Sr. ENCAD, Inc. - A Kodak Company email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Hawley, Eric [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 10:23 AM To: Perl-Win32 (E-mail) Subject: question about grep I got a question concerning grep. I would like to use it to pull out all full lines of text, that starts with the word "Status", from a list of files. I am not really too experienced with using Metacharacters and Metasymbols and do not know how to go about doing this with grep. Can someone help me out with this? Thanks in advanced Eric ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs