
format output from system command
--bcaec544ed08a256de04a0e78872
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I need help in formatting ouput from system command, i could'nt figure out a
way to format output from system command. appreciate your help with this,
the details are below.
hlis3 is file with list of clients
hosta
hostb
hostc
hostd
The below program looks through each client and outputs the class name,
the desired output needs to be in the below format
------------------------------------------------------------ -------------
hosta Solaris_DEV
hostb Solaris_DEV
hostc Archivelogs__DV
Archivelogs__DV
Solaris_DEV
hostd Archivelogs__DV
------------------------------------------------------------ ---------------
#!/usr/bin/perl
use strict;
use warnings;
open (FILEOUT, ">>cmdout") ||die "cant open cmdout: $! \n";
select (FILEOUT);
open(FILE, "hlis3") || die "Can't open hlis3: $!\n";
while (<FILE>) {
chomp;
print;
my $cmd = `bppllist -byclient $_ \|grep \"CLASS \" |awk \'\{print \$2\}\' `;
printf FILEOUT "%70s\n", $cmd;
}
------------------------------------------------------------ ------------
program output similar to this. need help in formating this as mentioned in
the begining
hosta Solaris_DEV
hostb Solaris_DEV
hostc Archivelogs__DV
Archivelogs__DV
Solaris_DEV
Regards
Sj
--bcaec544ed08a256de04a0e78872--
format output from system command
--bcaec5314c1531569d04a0ee3114
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I need help in formatting ouput from system command, i could'nt figure out a
way to format output from system command. appreciate your help with this,
the details are below.
hlis3 is file with list of clients
hosta
hostb
hostc
hostd
The below program looks through each client and outputs the class name,
the desired output needs to be in the below format
------------------------------------------------------------ -------------
hosta Solaris_DEV
hostb Solaris_DEV
hostc Archivelogs__DV
Archivelogs__DV
Solaris_DEV
hostd Archivelogs__DV
------------------------------------------------------------ ---------------
#!/usr/bin/perl
use strict;
use warnings;
open (FILEOUT, ">>cmdout") ||die "cant open cmdout: $! \n";
select (FILEOUT);
open(FILE, "hlis3") || die "Can't open hlis3: $!\n";
while (<FILE>) {
chomp;
print;
my $cmd = `bppllist -byclient $_ \|grep \"CLASS \" |awk \'\{print \$2\}\' `;
printf FILEOUT "%70s\n", $cmd;
}
------------------------------------------------------------ ------------
program output similar to this. need help in formating this as mentioned in
the begining
hosta Solaris_DEV
hostb Solaris_DEV
hostc Archivelogs__DV
Archivelogs__DV
Solaris_DEV
Regards
Sj
--bcaec5314c1531569d04a0ee3114--
Re: format output from system command
Hi jet speed,
On Friday 15 Apr 2011 00:23:17 jet speed wrote:
> Hi,
>
> I need help in formatting ouput from system command, i could'nt figure out
> a way to format output from system command. appreciate your help with
> this, the details are below.
>
> hlis3 is file with list of clients
>
> hosta
> hostb
> hostc
> hostd
>
> The below program looks through each client and outputs the class name,
>
> the desired output needs to be in the below format
>
> ------------------------------------------------------------ -------------
> hosta Solaris_DEV
>
> hostb Solaris_DEV
>
> hostc Archivelogs__DV
> Archivelogs__DV
> Solaris_DEV
>
> hostd Archivelogs__DV
>
> ------------------------------------------------------------ ---------------
>
> #!/usr/bin/perl
>
>
>
> use strict;
> use warnings;
>
> open (FILEOUT, ">>cmdout") ||die "cant open cmdout: $! \n";
> select (FILEOUT);
1. Don't use bareword file-handles.
2. Use the three-args open:
open (my $file_our, '>>', 'cmdout') or die "Cannot open cmdout: $!";
3. You shouldn't use select here.
>
>
> open(FILE, "hlis3") || die "Can't open hlis3: $!\n";
>
See above.
Maybe you'd like to read "Modern Perl" here:
http://perl-begin.org/books/#modern-perl
> while (<FILE>) {
>
Put the line in an explicit variable:
while (my $line = <$fh>) {
Also don't call variable names file:
http://perl-begin.org/tutorials/bad-elements/#calling-variab les-file
> chomp;
>
> print;
>
You've just printed it without the chomp.
>
> my $cmd = `bppllist -byclient $_ \|grep \"CLASS \" |awk \'\{print \$2\}\'
> `;
>
1. Why are you using grep and awk from within Perl, which can do all they can
do and more?
2. By using $_ inside the `...` you're opening yourself to a lot of shell-
script-code injection problems:
* http://shlomif-tech.livejournal.com/35301.html
* http://shlomif-tech.livejournal.com/14671.html
3. You are backslash-escaping many characters that do not need to be backslash
escaped inside a `...`.
4. The variable called $cmd is misleading because it's not a command, but its
output.
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/
<rindolf> I am not solvable. I am Turing hard.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: format output from system command
On Apr 15, 2011, at 2:11 AM, Shlomi Fish wrote:
> 1. Don't use bareword file-handles.
>
> 2. Use the three-args open:
>
> open (my $file_our, '>>', 'cmdout') or die "Cannot open cmdout: $!";
What about writing it like this:
open ('FILEOUT', '>>', 'cmdout') ||die "cant open cmdout: $! \n";
Is that O.K.?
Marc
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: format output from system command
Hello Marc,
> What about writing it like this:
>
> open ('FILEOUT', '>>', 'cmdout') ||die "cant open cmdout: $! \n";
>
> Is that O.K.?
You are still using a bareword filehandle.
Regards,
Alan Haggai Alavi.
--
The difference makes the difference
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: format output from system command
On Apr 15, 2011, at 8:37 AM, Alan Haggai Alavi wrote:
>> open ('FILEOUT', '>>', 'cmdout') ||die "cant open cmdout: $! \n";
>>
>> Is that O.K.?
>
> You are still using a bareword filehandle.
Putting single quotes around the filehandle allows it to pass =
Perl Critic, so I was just curious about the difference.
Thanks,
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: format output from system command
On 11-04-15 11:22 AM, sono-io [at] fannullone.us wrote:
> open ('FILEOUT', '>>', 'cmdout') ||die "cant open cmdout: $! \n";
>
> Is that O.K.?
The problem with not using lexical-scoped file handles is that if a
module opens a file using the same name, it closes yours. It's best if
you limit the scope of the file handle to the block of the open. You
can always pass the file handle to a subroutine:
sub foo {
my $fh = shift [at] _;
print $fh "Foo\n";
}
my $file = 'foo.tmp';
open my $out_fh, '>', $file or die "could not open $file: $!\n";
foo( $out_fh );
close $out_fh '>', $file or die "could not close $file: $!\n";
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: format output from system command
On Fri, 15 Apr 2011 08:53:12 -0700, sono-io wrote:
> On Apr 15, 2011, at 8:37 AM, Alan Haggai Alavi wrote:
>
>>> open ('FILEOUT', '>>', 'cmdout') ||die "cant open cmdout: $! \n";
>>>
>>> Is that O.K.?
>>
>> You are still using a bareword filehandle.
>
> Putting single quotes around the filehandle allows it to pass Perl
> Critic, so I was just curious about the difference.
If you're concerned about following the recommendations of Perl Best
Practices (which PerlCritic is based on), I'd hope you'd follow the one
that says to not create bareword filehandles.
I adopted the PBP recommendation to always wrap the lexical filehandle in=
curlies when using it as the indirect object, and I like the result a lot=
..
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/