Hi Monks ,

I need to process the output of a command which is in the format of a table. Depending on specified counter information , i need to extract entries corresponding to that counter.

An output instance is mentioned below :

Timestamp 10.72.184.159:cpu_busy -------------------------------------------------- 2010-01-05 22:49:03 1.707 2010-01-05 22:50:04 1.753 2010-01-05 22:51:03 1.994 2010-01-05 22:52:03 1.726 2010-01-05 22:53:03 1.783 2010-01-05 22:54:03 1.733 2010-01-05 22:55:03 1.742 2010-01-05 22:56:03 1.902 2010-01-05 22:57:03 1.902 Timestamp 10.72.184.159:disk_data_written ----------------------------------------------------- 2010-01-05 22:49:03 47.467 2010-01-05 22:50:04 43.148 2010-01-05 22:51:03 47.186 2010-01-05 22:52:03 45.867 2010-01-05 22:53:03 47.333 2010-01-05 22:54:03 47.067 2010-01-05 22:55:03 42.400 2010-01-05 22:56:03 46.533 Timestamp 10.72.184.159:disk_data_read --------------------------------------------------------- 2010-01-05 22:49:03 13.467 2010-01-05 22:50:04 10.557 2010-01-05 22:51:03 10.712 2010-01-05 22:52:03 10.733 2010-01-05 22:53:03 10.667 2010-01-05 22:54:03 12.667 2010-01-05 22:55:03 10.133 2010-01-05 22:56:03 10.000 2010-01-05 22:57:03 10.133

To extract the data with respect to the counter "10.72.184.159:disk_data_written" , i have written the following code where $cmd_out contains the output in a string:

my @out_arr = split (/\n/,$cmd_out); my @ts_arr; my $switch = 0; foreach (@out_arr){ if (/.*?10.72.184.159:disk_data_written.*/){ $switch = 1; } if(($switch == 1) && ($_ !~ /^\s*$/)){ push @ts_arr,$_; } if(($switch == 1) && (/^\s*$/)){ last; } } print "\n The timestamp array consists of : \n"; print @ts_arr;

Is there a more optimized way to achieve the same ? Thanks in advance.


In reply to Optimum method to perform data extraction in a table by perlpal

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.