HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl
Hi all,
Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM.
Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers.
At the moment am using
To re-direct the df output. Am using df -k because some of the Solaris and HP servers does not have df -h, by using df -k, am sure it will work on all of them.
Sample output is as below:
Then I get rid of the header as below, more dependence on using Unix OS commands, sorry Perl gurus, don't know what's the equivalent of the commands below in Perl :-)
Then I do the lines below which read each line to an array:
My question is, first of all, how to do an array operation where I can operate on the field/column 2,3,4 where I can divide them by 1024 or 1024/1024 so their KB values are converted to MB or GB? Or do I have to foreach each array member and do the division line by line? Would be nice if I can use the df header as hash references :-)
Also need to be able to get the max(length) of each column so I can use it for formatting the output and I can't find a Perl max or min function -
BTW, if anyone is interested to know what am trying to do, I've attached a version of the script in Korn shell.
Am wanting to convert it to Perl 'coz I have a server that has 30+ lines of df output and it takes ages to run using Korn shell. I am hoping that it will run faster in Perl. Plus it is a good exercise to learn Perl arrays and sorting?
Can anyone suggest the "best" Perl forum that I can post this question to? Any response/advise will be much appreciated.
Thanks in advance.
Sample output of the run using the Korn shell script as below, using df-m:
Then I get rid of the header as below, ... don't know what's the equivalent of the commands below in Perl
...
My question is, first of all, how to do an array operation where I can operate on the field/column 2,3,4 where I can divide them by 1024 or 1024/1024 so their KB values are converted to MB or GB? Or do I have to foreach each array member and do the division line by line? Would be nice if I can use the df header as hash references :-)
Also need to be able to get the max(length) of each column so I can use it for formatting the output and I can't find a Perl max or min function -
...
Sample output of the run using the Korn shell script as below, using df-m:
A sample Perl script for your problem is posted below. I'll answer your questions first.
To get rid of the header, skip the line if line number is 1. The statement with the "next" keyword (in the script below) does that.
You do not have to use "foreach" loop, since the array indexes are fixed. The "a" qualifier splits the input line into an array called "@F". We know that the elements at indexes 1, 2 and 3 are to be divided by 1024, so we do just hard-code those. How do we know that those indexes are fixed? Because the output of "df -k" has those columns in the pre-defined positions.
Perl has the min and max functions in the List::Util module, which is a part of the standard distribution (>= ver 5.6).
Although this is not really required for your case. Since we are anyway looping through all the lines, we just need to initialize a max length variable, and reset it if we encounter a bigger length of the first token.
The Perl script below works on the data in the file "f17".
Thanks for a very quick response. Very much appreciated, helpful and finally got something to move forward with. Ran the script that you provided on a server that has almost 200 defined filesystem, it ran for a minute using my shell script and took only 5 seconds using Perl.
Quote:
Originally Posted by durden_tyler
A sample Perl script for your problem is posted below. I'll answer your questions first.
To get rid of the header, skip the line if line number is 1. The statement with the "next" keyword (in the script below) does that.
Didn't know I can check for line numbers like this. Can I do the same check for files when reading each line of a file?
Quote:
You do not have to use "foreach" loop, since the array indexes are fixed. The "a" qualifier splits the input line into an array called "@F". We know that the elements at indexes 1, 2 and 3 are to be divided by 1024, so we do just hard-code those. How do we know that those indexes are fixed? Because the output of "df -k" has those columns in the pre-defined positions.
The "a" qualified that you referring to, is that the command line switch/option in perl -lane?
Quote:
Perl has the min and max functions in the List::Util module, which is a part of the standard distribution (>= ver 5.6).
Unfortunately, I still got some servers where the Perl is < 5.6 due to some old application that can't be upgraded and management don't want to touch whatever are installed on those servers.
And some of the 5.6 versions that I have, they do not have that module . Wish am the SA and I can install those modules.
Quote:
Although this is not really required for your case. Since we are anyway looping through all the lines, we just need to initialize a max length variable, and reset it if we encounter a bigger length of the first token.
The Perl script below works on the data in the file "f17".
Finally, you do not have to create a temporary file that stores the value of "df" command. You could simply pipe it to the Perl script:
Code:
df -k | perl -lane '... <script> ...'
tyler_durden
I did as below. Not sure if that is what you meant to say.
I created three (3) files, dfk.pl, dfm.pl and dfg.pl. All of them contains similar codes except for the $F[1] = int($F[1]/1024)."-MB"; lines, the dfg.pl have it at $F[1] = int($F[1]/1024/1024)."-MB"; and dfk.pl have it as simply $F[1] = int($F[1])."-KB";
Then I run df -k | dfm.pl. Output is as I want them to be. So thanks a lot. That looks good for the meantime.
If you don't mind, got some more additional questions.
Quote:
Finally, you do not have to create a temporary file that stores the value of "df" command. You could simply pipe it to the Perl script:
If I really need to store the output of the df command and "manipulate" the data in some way, can I re-direct the df output to an array, i.e. for example, @df=`df -k`? On some servers, the filesystem are owned by different business groups, so I need to check which filesystem belong to which business group and then send an email to whichever business group own the filesystem. I need to check the value of capacity and then make some computation of how much space need to be requested.
For the print format, RE: max length, you are checking for max length only for the first column and the others you set them to be at constant max length of either 10 or 20, is that correct?
From your codes below, you are storing the computed values of the @F array to the array named @x and then after that sort @x and assigning them to @y to get printed, is that correct?
...Didn't know I can check for line numbers like this. Can I do the same check for files when reading each line of a file?...
Yes, you can.
Quote:
Originally Posted by newbie_01
...The "a" qualified that you referring to, is that the command line switch/option in perl -lane?...
Yes, it is. Type
Code:
perl --help
for description of all switches.
Quote:
Originally Posted by newbie_01
...I did as below. Not sure if that is what you meant to say.
I created three (3) files, dfk.pl, dfm.pl and dfg.pl. All of them contains similar codes except for the $F[1] = int($F[1]/1024)."-MB"; lines, the dfg.pl have it at $F[1] = int($F[1]/1024/1024)."-MB"; and dfk.pl have it as simply $F[1] = int($F[1])."-KB"; ...
Any approach is fine if it solves your problem and is easily maintainable.
Quote:
Originally Posted by newbie_01
...If I really need to store the output of the df command and "manipulate" the data in some way, can I re-direct the df output to an array, i.e. for example, @df=`df -k`? On some servers, the filesystem are owned by different business groups, so I need to check which filesystem belong to which business group and then send an email to whichever business group own the filesystem. I need to check the value of capacity and then make some computation of how much space need to be requested. ...
Yes, you can redirect the output of df -k to a Perl array, but I don't see why you would need that. The processing steps you've mentioned could be performed quite easily by a Perl script that is piped to df -k.
Quote:
Originally Posted by newbie_01
...For the print format, RE: max length, you are checking for max length only for the first column and the others you set them to be at constant max length of either 10 or 20, is that correct?
...
That is correct.
Quote:
Originally Posted by newbie_01
...From your codes below, you are storing the computed values of the @F array to the array named @x and then after that sort @x and assigning them to @y to get printed, is that correct?
...
sub uniq {
my %h;
return grep { !$h{$_}++ } @_
}
The above code is to remove duplicates from array.
I am having hard time understanding below things (basically around highlighted code in bold)-
when was the value inserted in hash?
and are we only adding a key in Hash not... (1 Reply)
Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first...
find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
I am currently in Afghanistan and do not have access to some of the resources I normally do back in the US. Just accessed this site and it looks promising! Hopefully you will not find my question too much of a waste of your time.
I write mostly Korn Shell and PERL on Solaris systems for the... (2 Replies)
Hi,
i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script.
My textfile:
<Nr>tab<word>tab<other stuff>new line
6807 die ART.Acc.Sg.Fem
6426 der ART.Gen.Sg.Fem
2 die ART.Nom.Sg.Fem
87 auf APPR.--
486 nicht PTKNEG.--... (1 Reply)
This is about how to Monitoring folder for new files using shell script
im doing a project using smsserver tools 3. i have used a perl script
to handle incoming messages. the content of each message must be
directed to a java program. this program generates the answer to reply
to the user... (2 Replies)
Hi All,
I have an array in perl as @match = (201001,201002,201001,201002);
I am trying to sort this array as
@match = sort(@match);
print "@match";
I dont see the output sorted any answers
I also tried another way, but still the results are not sorted
foreach my $match (sort { $a... (2 Replies)
I need a shell script which will convert the given string within a <title> tag to Capitalize case.
E.g "<title>hi man: check this out</title>"
to "<title>Hi Man: Check This Out</title>" (11 Replies)
perl script:
my $logdir = '/smp/dyn/logfiles/fsm/mp/mp';
$logdir = $logdir ."/mp${toDate}*";
i tried to make it..as below .. but not working ..
date +%m%d%y
logdir = /smp/dyn/logfiles/fsm/mp/mp
logdir=$logdir/mp"$date"
but it was not working.....
can someone please help me out in... (1 Reply)
I have this script that I want to be able to scan a list of IP address from a file (namlist and snifflist)and Hostnames of network devices to see if they are alive or down and if telnet port 23 is open. I originally started to write this in Korn Shell and ran into some issue trying to closed a... (0 Replies)
Hi,
When using sort on an associative array:
foreach $key (sort(keys(%opalfabet))){
$value = $opalfabet{$key};
$result .= $value;
}
How does it handle double values?
It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)