Operating System - HP-UX
1847253 Members
4413 Online
110263 Solutions
New Discussion

Re: PERL Programming question .

 
baiju_3
Esteemed Contributor

PERL Programming question .

Hi All,

I am new to PERL . Trying to script something .


@list=`ls -al /dev/vg00'

now my array element is
crw-r----- 1 root sys 64 0x000000 Aug 15 2004 /dev/vg00/group


I do not want to store all the info, what I need is minor number and VG name .

I tried to include awk , like this

@list=`ls -ld /dev/vg00|awk '{print $6,$NF}'`;

But Perl is giving compilation error ,

I there any function to split the array element (similar we achive with awk )

substr function will not work for me , since all array elements do not have same size ...

Please reply ,
Thanks in advance,
BL.






Good things Just Got better (Plz,not stolen from advertisement -:) )
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: PERL Programming question .

Yes, just as in awk the function is called split. Man perlfunc for details.

Here's a snippet that will do what you are trying to do with your awk:

#!/usr/bin/perl -w

use strict;

my @list = `ls -la /dev/vg00`;
chomp(@list);
my $i = 0;
while ($i <= $#list)
{
my @array = split /\s+/,$list[$i];
if ($#array > 5)
{
printf("%s %s\n",$array[5],$array[$#array]);
}
++$i;
}

This could have been written far more tersely but I intentionally do it step by step for clarity. Note the use of $#array to yield the maximum allowed subscript of @array. \s+ says grab all the white space.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: PERL Programming question .

One thing that I should add is that Perl arrays are zero-based rather than awk's which are 1-based.
If it ain't broke, I can fix that.
David Child_1
Honored Contributor

Re: PERL Programming question .

Try something like:

(undef, undef, undef, undef, undef, $minornum, undef, undef, undef, $vgname) = qw(`ls -al /dev/vg00`);

I think you will need the 'qw' to break out each element.

Also, I believe there are perl built-in functions that can do this for you. I don't have my docs in front of me, but take a look at functions related to stat. I have used stat to get time stamps on files;

$time = (stat($file))[8];

so it would be something like;

($minornum, $vgname) = (stat(/dev/vg00/group))[7,9];

or even;

(undef, undef, undef, undef, undef, $minornum, undef, undef, undef, $vgname) = stat(/dev/vg00/group);

The elements of stat are not isn the same order as 'ls -l' so you will need to look at that.


David
A. Clay Stephenson
Acclaimed Contributor

Re: PERL Programming question .

While I'm at it, I'll go ahead and show you what was wrong with your awk approach:

@list=`ls -la /dev/vg00|awk '{print \$6,\$NF}'`;

You just needed to escape the $6 and $NF so that Perl would leave them alone.



If it ain't broke, I can fix that.
dirk dierickx
Honored Contributor

Re: PERL Programming question .

still using 'awk' in perl is pretty pointless, you might just as well write a shell script in that case...