On Jun 1, [EMAIL PROTECTED] said:
>I want to take the output of a df -k command and take the first column
>and put it into a nice array or list that I can then later put each
>seperate line into a command. I have tried a few different versions and
>can't figure it out. I'm new to this and I'm sure it is easy but well
>I'm green what can I say.
> I just want the out put if to be like if I did a df -k | awk ' {print $1}'
To do that, you want to split on whitespace, and get the first element
returned.
> foreach $_ (`df`){
> ($fs, $kb, $used, $avail, $cap, $mount) = split (" ",/(\S+\\/n));
> print $fs if /dsk/;
> }
You're splitting on whitespace but I don't know what that /(\S+\\/n) is
supposed to accomplish. You just want to split $_.
for (`df`) {
next if !/dsk/; # skip lines without "dsk";
my ($fs) = split(' ', $_);
print "$fs\n";
}
In fact, you can just say 'split', and Perl will split $_ on ' '.
for (`df`) {
next if !/dsk/;
print( (split)[0], "\n" );
}
The parentheses are needed around the arguments to print() for evil
reasons:
print (split)[0]
is parsed like
print(split) [0]
which doesn't make much sense to Perl -- you're taking a subscript of
something that isn't explicitly a list. It's rather crufty, but it's
Perl, so live with it.
Finally, if you'd like, you can use the grep() function (or even the grep
system call) to get rid of lines with 'dsk' ahead of time:
for (`df | grep dsk`) {
print( (split)[0], "\n" );
}
or
for (grep /dsk/, `df`) {
print( (split)[0], "\n" );
}
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **