in reply to Redirect a substring/desired values from a System output to a file in Perl

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.

Replies are listed 'Best First'.
Re^2: Redirect a substring/desired values from a System output to a file in Perl
by Murali_Newbee (Novice) on Jul 31, 2018 at 11:34 UTC
    Thank you