jayu_rao has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

My requirement is as below:

2015-02-27 16:35:29,832 [[48:1JK5DE9A]] INFO - performing task 2015-02-27 16:39:31,850 [[50:1EB1DB9F]] INFO - text: [[abcd.this is no +t needed]] mystring 2015-02-27 16:45:24,250 [[49:3CE1DB9A]] INFO - text: [[efghijk.this po +rtion is also not needed]] mystring 2015-02-27 16:55:29,832 [[48:1JK5DE9A]] INFO - task done. 2015-02-27 16:55:31,850 [[50:1EB1DB9F]] INFO - text: [[lmn. again not +needed]] mystring 2015-02-27 16:57:13,435 [[50:1EB1DB9F]] INFO - text: [[lmn. repetition +. Again not needed]] mystring

I need to extract the portion with abcd, efghijk, lmn where ever the "mystring" appears in the file and print only their unique occurrences. I have written the below code but getting error:

perl log.pl Can't use an undefined value as a symbol reference at log.pl line 30, +<$_[...]> line 532 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as a symbol reference at log.pl l +ine 30, <$_[...]> line 532. at log.pl line 30
#!/usr/bin/perl -w use strict; use warnings; use autodie; use diagnostics; open my $read_log, '<', '/home/jay/test_output/tmp_app.log' or die $!; my $pattern = 'mystring'; my @log_lines=<$read_log>; my @all_lines_occurrences; my @all_extracts; my @unique_values; for(@log_lines){ if ($_ =~ m/.*$pattern.*/) { push (@all_lines_occurrences, $_); #print $_ "\n"; } } for (@all_lines_occurrences) { /^\d+\-\d+\-\d+ \d+:\d+:\d+\,\d+ \[(.*)\] .* \[(.*)\.(.*)\] .*\\n/; push (@all_extracts, $2); #print "target type: $2 \n"; } for(@all_extracts){ print $_ "\n"; } my @unique_values = do { my %seen; grep { !$seen{$_}++ } @all_extracts + }; print "\n Unique values are:\n"; for (@unique_values) { print $_ "\n"; } close $read_log;

Can you please tell what is wrong?

Regards,

Jay

Replies are listed 'Best First'.
Re: Need help with string extraction
by hdb (Monsignor) on Mar 20, 2015 at 08:12 UTC

    I can only guess which line causes the error, but in this line you have lost a comma:

    print $_ "\n";
Re: Need help with string extraction
by Anonymous Monk on Mar 20, 2015 at 07:52 UTC

    Can you please tell what is wrong?

    The error message tells it pretty good --- look around line 30, find your syntax error, see print if you're not sure what the mistake is

      Thanks :) got it.