Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pass a stdout to an array & parse it to get a specific line?

0 views
Skip to first unread message

lazyb...@yahoo.com

unread,
Dec 20, 2005, 2:41:43 PM12/20/05
to
Hi guru,

I'm trying to write a perl script which will read the input file,
capture the stdout, do a pattern seaching for a specific line in
stdout, and run another command on its line. So far, I'm able to get
my script to read the input file & display the output properly.
However, I 'm unable to pass the stdout to an array & parse it to get a
specific line. In other words, the code will stop working after the 1st
$CLI_PATH. Could someone pls help me out?

Here is the script:

#!/usr/bin/perl -w
use strict;
use warnings;
my $CLI_PATH="/bin/cr_cli";
my $hostFile = "test.out";
open (fhHostFile, $hostFile) || die "Cannot open $hostFile : $!\n";

while (<fhHostFile>) {
my $hostInfo=$_;
next unless $. >3; # skip the 1st 3 tab
my @host_info=split /\|/, $hostInfo;
my @output = `$CLI_PATH -cmd host.info -s file:admin -ID
NM:$host_info[2]`;
open (fhOutputLines, @output) || die "Cannot read @output : $!\n";
while (<fhOutputLines>) {
my $output = $_;
my $appID = `grep "^[ ]*|[0-9]" @output | awk -F'|' '{print
$2}'`;
my $cmd = sprintf("%s -cmd retrieve-host.info -s %s -ID
%s",
$CLI_PATH,
"file:admin",
$appID);
print "Running: $cmd\n";
system ($cmd);
}
close (fhOutputLines);

}
close (fhHostFile);

This is the output of the 1st $CLI_PATH if it's successful:

ID: 129147032122-1103152530110-00216-1024424668
Name: peterpan.central
Description:
OS: SunOS (Supported)
OS Version: 5.9
OS Architecture: sparc
Last Prepared: Host is not prepared
Virtual: false
Hidden: false
Type ID: 010010001024-0000000000000-00001-0000000004
Attributes:
<Table is empty>
Applications:

|---------------------------------------------|-----|--------------------|
|ID |Type |Parameters
|

|---------------------------------------------|-----|--------------------|
|129147032122-1103152530119-00217-0648912818 |RA |
|
|129147032122-1103152530125-00218-0960580121 |LD |
|

|---------------------------------------------|-----|--------------------|
.............

Above is the appID that I want to capture every time. Note, the appID
is changing everytime the 1st $cli_path reads a new host & retrieve it.

Any helps are greatly appreciated.
Thx,
-Chris

use...@davidfilmer.com

unread,
Dec 20, 2005, 3:43:04 PM12/20/05
to
lazyb...@yahoo.com wrote:
> my @output = `$CLI_PATH -cmd host.info -s file:admin -ID NM:$host_info[2]`;

OK, that puts the output of the command into the array @output. So far,
so good...

> open (fhOutputLines, @output) || die "Cannot read @output : $!\n";

@output is an array, not a file... you don't need to open anything
here... omit that statement

> while (<fhOutputLines>) {
> my $output = $_;

You aren't reading a filehandle - you're iterating an array. Try this
instead:

foreach my $output( @output ) {

Cheers!

--
http://DavidFilmer.com

Paul Lalli

unread,
Dec 20, 2005, 3:52:47 PM12/20/05
to
lazyb...@yahoo.com wrote:
> I'm trying to write a perl script which will read the input file,
> capture the stdout, do a pattern seaching for a specific line in
> stdout, and run another command on its line. So far, I'm able to get
> my script to read the input file & display the output properly.
> However, I 'm unable to pass the stdout to an array

This statement doesn't make any sense. You pass things to subroutines.
An array is simply assigned to.

> & parse it to get a
> specific line. In other words, the code will stop working after the 1st
> $CLI_PATH.

"Stop working" is a terrible problem description. What happens? What
output do you see that you don't want, or don't see that you do want?
What error messages do you receive?

> Could someone pls help me out?
>
> Here is the script:
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> my $CLI_PATH="/bin/cr_cli";
> my $hostFile = "test.out";
> open (fhHostFile, $hostFile) || die "Cannot open $hostFile : $!\n";
>
> while (<fhHostFile>) {
> my $hostInfo=$_;
> next unless $. >3; # skip the 1st 3 tab
> my @host_info=split /\|/, $hostInfo;
> my @output = `$CLI_PATH -cmd host.info -s file:admin -ID
> NM:$host_info[2]`;
> open (fhOutputLines, @output) || die "Cannot read @output : $!\n";
> while (<fhOutputLines>) {
> my $output = $_;

This makes no sense of any kind. You open files. You do not open
arrays. Simply loop through the array. Didn't you get any kind of
error message here?

foreach my $output (@output) {

> my $appID = `grep "^[ ]*|[0-9]" @output | awk -F'|' '{print $2}'`;

I'd be shocked if this worked correctly. Back-ticks interpolate
variables, just as double-quotes do. If you want to use awk's $2, you
have to escape it. Otherwise, you're using Perl's $2.

Also, I have no idea what @output is doing there. Don't you want the
current output line you're looking for? That would be $output in your
example.

More to the point, is there any particular reason you're not just doing
this bit of processing in Perl, rather than shelling out to grep and
awk?

Paul Lalli

0 new messages