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

Hello my friends

foreach(@{$arr_ref}) { @hlist=(@$_); foreach my $lines(@hlist){ open (my $file, '>>', 'hpart.txt') or die "Could not open file +: $!"; my $output = `my system command`; die "$!" if $?; print $file $output; } }

this code takes a list and executes a system command which gives output in hpart.txt as following :

something something$ something12

something something something

_id:ac43434

something something something something

something something$ something12

something something something

_id:ci2784

something something something something

etc etc

desired ouput:

ac4334

ci2784

as far as now, after my script executes, i grep the output file i.e hpart.txt with "_id:" and get the list and clear the "_id:" in editplus

But how can i redirect only those values to the output file??

Thank you in advance my friends

  • Comment on Redirect a substring/desired values from a System output to a file in Perl
  • Download Code

Replies are listed 'Best First'.
Re: Redirect a substring/desired values from a System output to a file in Perl
by Discipulus (Canon) on Jul 27, 2018 at 08:44 UTC
    Hello Murali_Newbee and welcome to the monastery and to the wonderful world of Perl!

    You can use open using the pipe | at the end of the command to read from a command output and then parse each line.

    Like in:

    open my $cmd, "/bin/cmd|" or die "something useful here like $^E"; while (<$cmd>){ chomp; print $append_file "$1\n" if $_ =~ /_id:(\S)+$/; #please re-check t +he regex.. }

    L*

    PS in the same way you can write to a program STDIN putting the pipe in front of the program like open my $sendmail, "|/usr/sbin/sendmail etc etc";

    The above example is the basic way to do what do you ask for but many modules on cpan can achieve the same in different, perhaps more robust, way.

    Also note that you can open your append file once and outside of the loop: you reopen it many times and you profit of the automatic close when $file goes out of the scope: do not take this habit! use the explicit close as soon as you do not need the filehandle anymore.

    More: if you just want to append to a single file during all the program you can use select $filehandle to have all print calls redirected to such file.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Hi Discipulus, with my previous script i achieved the output to the file i mentioned, the problem here is i get the bunch of output, where i need an id, could you please help me with the Regular expression?
Re: Redirect a substring/desired values from a System output to a file in Perl
by haukex (Archbishop) on Jul 27, 2018 at 08:57 UTC

    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.

      Thank you
Re: Redirect a substring/desired values from a System output to a file in Perl
by hippo (Archbishop) on Jul 27, 2018 at 09:03 UTC
    But how can i redirect only those values to the output file??

    Your scalar variable $output is just like any other variable. You can manipulate and modify it in any way you choose before printing it to the output file. If this actual modification is the part you are having trouble with, then for now ignore performing input and output and just work on that. Post an SSCCE if you still have trouble and maybe take a read through How to ask better questions using Test::More and sample data to help others understand what it is you want to achieve. eg:

    use strict; use warnings; use Test::More tests => 1; my $have = '_id:ci2784'; my $want = 'ci2784'; # ... some code here which modifies $have is ($have, $want);

    Good luck!