Assuming that the output fits in memory, you can get the output as an array of lines by using qx// aka `` in list context, and then use grep. However, I would recommend using capturex from IPC::System::Simple because there are potential security issues with qx//, or, if the output can become too large to sensibly fit into memory, there are other methods/modules to run the external program and get its output line-by-line, similar to what Discipulus showed. See my post on these topics here (with lots of example code).

use warnings; use strict; use IPC::System::Simple qw/capturex/; my @cmd = ('cat', 'sample.txt'); # just an example print map { s/^_id://; $_ } grep { /^_id:/ } capturex(@cmd);

Note that map { s/^_id://; $_ } grep { /^_id:/ } can be written more briefly as grep { s/^_id:// }, although people often recommend against using grep for its side-effects.


In reply to Re: Redirect a substring/desired values from a System output to a file in Perl by haukex
in thread Redirect a substring/desired values from a System output to a file in Perl by Murali_Newbee

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.