CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by Jeff29

 

First page Previous page 1 2 Next page Last page  View All


Jeff29
Novice

Oct 8, 2010, 11:08 AM

Post #1 of 26 (4526 views)
Post deleted by Jeff29

 


BillKSmith
Veteran

Oct 8, 2010, 11:49 AM

Post #2 of 26 (4510 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

You should search for similar questions. The thread http://perlguru.com/gforum.cgi?post=48241 should answer the excel part of your question. The parsing is a straightforward use of regular expressions. Show us what you have tried.
Good Luck,
Bill


Jeff29
Novice

Oct 8, 2010, 11:55 AM

Post #3 of 26 (4505 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

HI Bill,



Tried the following,I fail to understand on how to extract the perforcepath: when there is an ERROR: .Please help

Code
   #!/usr/bin/perl -w 
use strict;
use Spreadsheet::WriteExcel;
my $match;
my $workbook = Spreadsheet::WriteExcel->new("Tech_BlacklistedCRs.xls");
my $worksheet = $workbook->addworksheet();
my $cr_line = '';
open my $line, '<', 'File.txt' or die 'Cannot open file';

while (my $match = <$line1>)
{
if ($match =~ /CR:/){
$cr_line = $match;
#print $cr_line;

}

elsif ($match =~ /Error:/){


if ($match =~ /Perforce path:/) {

#print $cr_line, $match;
$worksheet->write($i, 0, $cr_line);
$worksheet->write($i, 1 , $match);
$cr_line = '';
$i++;
#$j++;

}
}

$workbook->close();



(This post was edited by Jeff29 on Oct 8, 2010, 2:21 PM)


BillKSmith
Veteran

Oct 8, 2010, 1:49 PM

Post #4 of 26 (4494 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

   

You must create a worksheet object before you can use it.

Remove the test for ERROR. Lines that do not match 'CR' or 'Perforce path' will be ignored.

You must declare the variables $match and $cr_line. They should be declared with 'my' inside the while loop, but before they are used.

Within each if block, you need a regular expression to extract the data that you want from the line.
Good Luck,
Bill


Jeff29
Novice

Oct 8, 2010, 2:17 PM

Post #5 of 26 (4492 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

I will take care of syntax errors,but my point it I only want to extract the CR, all perforce paths after an error: until the next CR: How do I change the above logic to do that?Pls help


(This post was edited by Jeff29 on Oct 8, 2010, 2:22 PM)


BillKSmith
Veteran

Oct 8, 2010, 8:35 PM

Post #6 of 26 (4483 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

This should work as long as every CR line has a Perforce line. All other lines are ignored.


Code
LINE: 
while ( my $match = <$line1> ) {
my $cr_line;
my $match;
if ( $match =~ /CR:/ ) {
$cr_line = $match;
print $cr_line;
next LINE;
}

if ( $match =~ /Perforce path:/ ) {
print $cr_line, $match;
$worksheet->write( $i, 0, $cr_line );
$worksheet->write( $i, 1, $match );
$cr_line = '';
$i++;
next LINE;
}

}
$workbook->close();

Good Luck,
Bill


Jeff29
Novice

Oct 8, 2010, 9:50 PM

Post #7 of 26 (4478 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

I think you are missing a point here?? Please look at my input.I only want to print perforce paths that have an "ERROR:" before them.Did we take care of this conditon?Thanks for your help.


BillKSmith
Veteran

Oct 9, 2010, 7:39 AM

Post #8 of 26 (4466 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

You are right. I did not understand why you were interested in 'Error'. BTY that was not the only error in my previous post. The scope of variables was not right.

As I now understand the problem, you have a log file which contains both errors and warnings. You wish to extract the number and message from the error entries, but not the warnings.

When the following code detects a message, it outputs it and the most recent CR number, but only if an 'error' line was seen after the CR line.


Code
#!/usr/bin/perl -w  
use strict;
use warnings;
use Spreadsheet::WriteExcel;
open my $line, '<', 'File.txt' or die 'Cannot open file';
my $cr_line;
my $is_an_error_entry;
LINE:
while ( my $match = <$line1> ) {
if ( $match =~ /CR:/ ) {
$cr_line = $match;
print $cr_line;
$is_an_error_entry = 0;
next LINE;
}

if ($match =~ /Error:/) {
$is_an_error_entry = 1;
next LINE;

if ( $match =~ /Perforce path:/ ) {
if ($is_an_error_entry) {
print $cr_line, $match;
$worksheet->write( $i, 0, $cr_line );
$worksheet->write( $i, 1, $match );
}
$cr_line = '';
$i++;
}

}
$workbook->close();

Good Luck,
Bill


Jeff29
Novice

Oct 9, 2010, 3:31 PM

Post #9 of 26 (4457 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Thanks Bill ,seems like there is a "}" missing after

Code
if ($match =~ /Error:/) {  
$is_an_error_entry = 1;
next LINE;
/****missing brace****/

Anyway I took care.

but I need slight modification.Currently my output is like below.

Set CR: 12345 Perforce path: //Depot/old/radio/bit/audradio/rel/perl/src/profile.c#3

I want to remove the "Set CR:" and "Perforce path:" from the output.Can you please suggest ,how do I do that?


BillKSmith
Veteran

Oct 9, 2010, 5:26 PM

Post #10 of 26 (4451 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

I mentioned that in a previous post. See the fix below.

You still may have another problem. Your sample of data has two Perforce lines for the last CR. If you delete the line which clears $cr_line, you will reuse the old CR for the second Perforce.

I added chomps to improve the printing. I do not know wheter or not you need the newlines in the spreadsheet.


Code
#!/usr/bin/perl -w  
use strict;
use Spreadsheet::WriteExcel;
open my $line, '<', 'File.txt' or die 'Cannot open file';
my $cr_line;
my $i;
my $is_an_error_entry;
LINE:
while ( my $match = <$line> ) {
if ( $match =~ /CR:/ ) {
$cr_line = (split /:\s*/, $match)[1];
chomp $cr_line;
$is_an_error_entry = 0;
next LINE;
}

if ($match =~ /Error:/) {
$is_an_error_entry = 1;
next;
}

if ( $match =~ /Perforce path:/ ) {
if ($is_an_error_entry) {
$match = (split /:\s*/, $match)[1];
chomp $match;
# print $cr_line, $match, "\n";
$worksheet->write( $i, 0, $cr_line );
$worksheet->write( $i, 1, $match );
$i++;
}
$cr_line = '';
}

}
#$workbook->close();

Good Luck,
Bill


Jeff29
Novice

Oct 9, 2010, 7:33 PM

Post #11 of 26 (4441 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Thanks a lot Bill,the above script suits my needs perfectly


Jeff29
Novice

Oct 12, 2010, 8:40 PM

Post #12 of 26 (4398 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

A small query,In my log file I have a line that contains http: links like below after the error:.I want to print and write to excel these links also .My question is there a easier way just to get a link from a line?

I added the following piece ,but am getting "Use of uninitialized value in pattern match (m//) at error.pl line 26"warnings

Code
    if ($match =~ /http:/) { 
if ($is_an_error_entry) {
$resolution_link = /http:.*/ --->Is this right?
chomp $resolution_link;
}
}

if ( $match =~ /Perforce path:/ ) {
if ($is_an_error_entry) {
$match = (split /:\s*/, $match)[1];
chomp $match;
print $cr_line, $match, $resolution_link, "\n";
$worksheet->write( $i, 0, $cr_line );
$worksheet->write( $i, 1, $match );
$worksheet->write( $i, 2, $resolution_link );
$i++;
}
$cr_line = '';
$resolution_link = ' ';
}


Code
INPUT:- 
Error: Error MSG
-----------*some data-----------
This is an error file please check http://wiki.com/error-resolution/fixes/fix31
Perforce path: //Depot/old/radio/bit/audradio/rel/perl/src/profile.c#3



(This post was edited by Jeff29 on Oct 12, 2010, 11:07 PM)


BillKSmith
Veteran

Oct 13, 2010, 6:44 AM

Post #13 of 26 (4363 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post


Quote

Code
        $resolution_link = /http:.*/ --->Is this right?






No, your text is in the variable $match, not $_. Your error message is telling you that $_ is not initialized. In scalar context, the match operator returns the number of matches (in this case, either 0 or 1). In list context, it returns an element for each set of capturing parenthesis (in this case, only one elemement - the link).

Code
           ($resolution_link) = $match =~ /(http:.*)/;


Good Luck,
Bill


Jeff29
Novice

Oct 13, 2010, 1:26 PM

Post #14 of 26 (4354 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Gotcha!!

My problem now is am printing everything (.*)after the link but I just want only the link

Code
 
($resolution_link) = $match =~ /(http:.*)/;-->Pls suggest on how to modify this line


For example,currently with the above code,the resolution_link gets pritnted as follows
http://wiki.com/error-resolution/fixes/fix31.Do not send e-mail for this issue

where as I only want
http://wiki.com/error-resolution/fixes/fix31



(This post was edited by Jeff29 on Oct 14, 2010, 2:34 PM)


Jeff29
Novice

Oct 14, 2010, 1:33 AM

Post #15 of 26 (4327 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

My problem now is am printing everything (.*)after the link but I just want only the link

Code
 
($resolution_link) = $match =~ /(http:.*)/;-->Pls suggest on how to modify this line


For example,currently with the above code,the resolution_link gets pritnted as follows
http://wiki.com/error-resolution/fixes/fix31.Do not send e-mail for this issue

where as I only want
http://wiki.com/error-resolution/fixes/fix31



(This post was edited by Jeff29 on Oct 14, 2010, 3:38 PM)


BillKSmith
Veteran

Oct 14, 2010, 11:55 AM

Post #16 of 26 (4319 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

I am confused. Your text says that the script prints everything after the link. Your example also shows extraneous printing before the link. Which is correct?

I believe that the first case is true. To fix this problem, fix the RE to include only the link. If you can be sure that there is always a whitespace character immediately after the link, all you have to do is look for it.


Code
($resolution_link) = $match =~ /(http:\S*)/;



Otherwise, you must know a lot more about the format of the link and/or of the line in the file.
Good Luck,
Bill


Jeff29
Novice

Oct 14, 2010, 2:31 PM

Post #17 of 26 (4317 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Sorry,the text is right.

After the link .I have "-" and "." and " "(space) some times.Running short of ideas on how to handle this?Googled a lot couldnt find an exact way to just extract the link from a line.Appreiciate any help

Thanks


(This post was edited by Jeff29 on Oct 14, 2010, 2:57 PM)


BillKSmith
Veteran

Oct 14, 2010, 3:11 PM

Post #18 of 26 (4308 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Your example does not conform to that description. Its clearly impossible to write a regular expression to extract any link from any line. Get an accurate description, and then fix the regular expression. (You will get whatever matches the part of the pattern between the parens.)
Good Luck,
Bill


Jeff29
Novice

Oct 18, 2010, 1:51 PM

Post #19 of 26 (4261 views)
Post deleted by Jeff29 [In reply to]

 


Jeff29
Novice

Oct 20, 2010, 12:03 PM

Post #20 of 26 (4245 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

1.I found a way to just match the link,all the links end with a number.I used the below regex

Code
($resolution_link) = $match =~ /(http:.*\d)/;

but the problem with the above regex is its matching all the numbers like below.
http://wiki/fixlink/error43. 3456
I only want to match the first number like below
http://wiki/fixlink/error43

2.I only want to print unique CRs,no duplicates. Is there an unique function something like that.Please help


BillKSmith
Veteran

Oct 20, 2010, 12:31 PM

Post #21 of 26 (4243 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Stick with a regular expression. Your problem now is that /.*/ matches the digits as well as almost everything else. It is also 'greedy', that is, it finds the longest string that matches the pattern. Your file names are probably always followed by whitespace. Make the greedy work for you by testing for that whitespace outside the capturing parens.


Quote
2.I only want to print unique CRs,no duplicates.



If a duplicate exists, do you want to ignore both of them because they are not unique, or do you want to ignore one of them, making the other unique.
Good Luck,
Bill


Jeff29
Novice

Oct 22, 2010, 10:34 AM

Post #22 of 26 (4210 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

 
Thanks for the suggestion,May be I should make it non-greedy by ".*?\d" .I will try this.


Quote

If a duplicate exists, do you want to ignore both of them because they are not unique, or do you want to ignore one of them, making the other unique.


I want to ignore only one of them


Jeff29
Novice

Oct 25, 2010, 11:55 AM

Post #23 of 26 (4189 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

Any pointers on how to avoid printing duplicates?

Thanks


BillKSmith
Veteran

Oct 25, 2010, 12:14 PM

Post #24 of 26 (4063 views)
Re: [Jeff29] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Refer to the uniq function in module List::Util. (ref perldoc List::Util).
Good Luck,
Bill


Jeff29
Novice

Oct 27, 2010, 2:42 PM

Post #25 of 26 (4057 views)
Re: [BillKSmith] Parsing a text file and outputting to an excel sheet [In reply to] Can't Post

Hi Bill,

I used uniq function but still I see duplicates getting printed,any idea what's the reason?

use List::MoreUtils qw( uniq );

Code
 print uniq($cr_line), $match, $resolution_link, "\n";


First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives