in reply to Effective way in perl

You seem to be running the same command two times, just to have its output stored in two distinct output files. Apart from that, I'd be worried about the risks of running shell commands that way, unless there is very hight certainty that the perl variables being used to build the command are "safe" (contain no shell metacharacters, etc).

In any case, it seems like you only need a shell to run the initial part of your pipeline, and it would be just as well for perl to do the rest:

my @command = ( $swlist, '-l', 'depot', '@', $depot_locn ); my $child_pid = open( my $shell, '-|', @command ) or die "shell command failed: $!\n"; open( my $out1, ">>", $log_std_depot ) or die "$log_std_depot: $!\n"; open( my $out2, ">>", $temp_log ) or die "$temp_log: $!\n"; print $out1 "COMMAND: @command ## FILTERS: $rel_string / $i / AR\n"; while (<$shell>) { next unless ( /$rel_string/ and /$i/ and /AR/ ); print $out1 $_; print $out2 $_; } close $out1; close $out2; close $shell;

Update: note that by doing the "grep" parts inside perl, you are able to make use of perl's much more powerful regex syntax -- you can do things that would not be possible with the standard command-line grep tool.

Another update: changed the code snippet to open the two output file in append mode (>> as per the OP code) rather than "truncate" mode.

Replies are listed 'Best First'.
Re^2: Effective way in perl
by Rajsri (Initiate) on Jul 16, 2009 at 15:00 UTC
    Hi Graff , Thanks for the suggestions. I tried running your code inside perl script. When i execute the above code , I see only below lines in the log file. (log_std_depot) COMMAND: /usr/sbin/swlist -l depot @ sidedepot.fc.com ## FILTERS: 0909 / / AR And also $temp_log seem to be empty. The commands are not getting executed, it only outputs the above line in the logfile. Are we missing anything here.
      The fact that your "log_std_depot" log file is showing two slashes in a row indicates that you are not setting $i. Apart from that, if you take the command line string from that log file ("/usr/sbin/swlist -l depot @ sidedepot.fc.com") and run it manually in a shell, what happens?

      If you make changes in the command string in order to get it to work in the shell, just make sure you apply the same changes in the perl script.

        I replaced $i with 1111 and ran the perl script, still I am getting below lines in the output file. Its not actually getting executed. COMMAND: /usr/sbin/swlist -l depot @ sidedepot.fc.com ## FILTERS: 0909 / 1111 / AR However if I run the same command on command line, its working fine. I am using the below command in the command line. /usr/sbin/swlist -l depot @ sidedepot.fc.com | grep 0909 | grep 1111 | grep AR In the script, we are not using grep, could this be the reason next unless ( /$rel_string/ and /$i/ and /AR/ ) ?????
        Hi graff, The below code is not working. next unless ( /$rel_string/ and /$i/ and /AR/ ); If i give only next unless ( /$rel_string/ ), it works fine.