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


In reply to Need help with string extraction by jayu_rao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.