Sponsored Content
Top Forums Shell Programming and Scripting HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl Post 302567046 by durden_tyler on Saturday 22nd of October 2011 01:02:42 AM
Old 10-22-2011
Quote:
Originally Posted by newbie_01
...

Code:
 
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d1       3099287 2482045  555257    82%    /
/proc                      0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
fd                         0       0       0     0%    /dev/fd
/dev/md/dsk/d3       3099287 1595167 1442135    53%    /var
swap                 8663192     368 8662824     1%    /var/run
swap                 8717624   54800 8662824     1%    /tmp
/dev/md/dsk/d4       5003466 4330989  622443    88%    /opt
dev0ns951:/vol/vol_admin/common 321912832 266888556 55024276    83%    /nas_mnt/common
dev0ns951:/vol/vol_admin/admin/cpadocs 39741440 32961924 6779516    83%    /opt/info
dev0ns951:/vol/vol_admin/admin 39741440 32961924 6779516    83%    /nas_mnt/admin
dev0ns951:/vol/vol_admin/docs 8468480 7245924 1222556    86%    /nas_mnt/docs
dev0ns951:/vol/vol_admin/prodhome 33658880 26586948 7071932    79%    /nas_mnt/prodhome
dev0ns951:/vol/vol_admin/saphome   92160   37960   54200    42%    /nas_mnt/saphome
dev0ns951:/vol/vol_admin/prodhome 33658880 26586948 7071932    79%    /home/users

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 Smilie-

...
Sample output of the run using the Korn shell script as below, using df-m:

Code:
 
Filesystem                                        MBytes      Used     Avail Capacity Mount
---------------------------------------------  --------- --------- --------- -------- -----------------------------------
/dev/md/dsk/d1                                   3027-MB   2424-MB    542-MB      82% /
/dev/md/dsk/d3                                   3027-MB   1560-MB   1406-MB      53% /var
/dev/md/dsk/d4                                   4886-MB   4229-MB    608-MB      88% /opt
/proc                                               0-MB      0-MB      0-MB       0% /proc
dev0ns951:/vol/vol_admin/docs                    8271-MB   7076-MB   1194-MB      86% /nas_mnt/docs
dev0ns951:/vol/vol_admin/prodhome               32870-MB  25964-MB   6906-MB      79% /home/users
dev0ns951:/vol/vol_admin/prodhome               32870-MB  25964-MB   6906-MB      79% /nas_mnt/prodhome
dev0ns951:/vol/vol_admin/saphome                   91-MB     37-MB     53-MB      42% /nas_mnt/saphome
fd                                                  0-MB      0-MB      0-MB       0% /dev/fd
mnttab                                              0-MB      0-MB      0-MB       0% /etc/mnttab
swap                                             8460-MB      0-MB   8459-MB       1% /var/run
swap                                             8513-MB     54-MB   8459-MB       1% /tmp

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).

Code:
$
$
$ perl -le 'BEGIN{use List::Util qw(min max)} @x = qw (10 20 -1 99 78); print "Min = ",min @x; print "Max = ",max @x'
Min = -1
Max = 99
$
$

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".

Code:
$
$
$ cat f17
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d1       3099287 2482045  555257    82%    /
/proc                      0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
fd                         0       0       0     0%    /dev/fd
/dev/md/dsk/d3       3099287 1595167 1442135    53%    /var
swap                 8663192     368 8662824     1%    /var/run
swap                 8717624   54800 8662824     1%    /tmp
/dev/md/dsk/d4       5003466 4330989  622443    88%    /opt
dev0ns951:/vol/vol_admin/common 321912832 266888556 55024276    83%    /nas_mnt/common
dev0ns951:/vol/vol_admin/admin/cpadocs 39741440 32961924 6779516    83%    /opt/info
dev0ns951:/vol/vol_admin/admin 39741440 32961924 6779516    83%    /nas_mnt/admin
dev0ns951:/vol/vol_admin/docs 8468480 7245924 1222556    86%    /nas_mnt/docs
dev0ns951:/vol/vol_admin/prodhome 33658880 26586948 7071932    79%    /nas_mnt/prodhome
dev0ns951:/vol/vol_admin/saphome   92160   37960   54200    42%    /nas_mnt/saphome
dev0ns951:/vol/vol_admin/prodhome 33658880 26586948 7071932    79%    /home/users
$
$
$
$ perl -lane 'next if $.==1;
              $maxlen = length($F[0]) if length($F[0]) > $maxlen;
              $F[1] = int($F[1]/1024)."-MB";
              $F[2] = int($F[2]/1024)."-MB";
              $F[3] = int($F[3]/1024)."-MB";
              @{$x[$i++]} = @F;
              END {
                $fmt = "%-${maxlen}s  %10s  %10s  %10s  %10s  %-20s\n"; print;
                printf ($fmt, "Filesystem", "MBytes", "Used", "Avail", "Capacity", "Mount");
                printf ($fmt, "-"x${maxlen}, "-"x10, "-"x10, "-"x10, "-"x10, "-"x20);
                @y = map $_->[0], sort { $a->[1] cmp $b->[1] } map [ $_, $_->[0] ], @x;
                foreach $item (@y) { printf ($fmt, @$item) } print;
              }
             ' f17

Filesystem                                  MBytes        Used       Avail    Capacity  Mount
--------------------------------------  ----------  ----------  ----------  ----------  --------------------
/dev/md/dsk/d1                             3026-MB     2423-MB      542-MB         82%  /
/dev/md/dsk/d3                             3026-MB     1557-MB     1408-MB         53%  /var
/dev/md/dsk/d4                             4886-MB     4229-MB      607-MB         88%  /opt
/proc                                         0-MB        0-MB        0-MB          0%  /proc
dev0ns951:/vol/vol_admin/admin            38810-MB    32189-MB     6620-MB         83%  /nas_mnt/admin
dev0ns951:/vol/vol_admin/admin/cpadocs    38810-MB    32189-MB     6620-MB         83%  /opt/info
dev0ns951:/vol/vol_admin/common          314368-MB   260633-MB    53734-MB         83%  /nas_mnt/common
dev0ns951:/vol/vol_admin/docs              8270-MB     7076-MB     1193-MB         86%  /nas_mnt/docs
dev0ns951:/vol/vol_admin/prodhome         32870-MB    25963-MB     6906-MB         79%  /nas_mnt/prodhome
dev0ns951:/vol/vol_admin/prodhome         32870-MB    25963-MB     6906-MB         79%  /home/users
dev0ns951:/vol/vol_admin/saphome             90-MB       37-MB       52-MB         42%  /nas_mnt/saphome
fd                                            0-MB        0-MB        0-MB          0%  /dev/fd
mnttab                                        0-MB        0-MB        0-MB          0%  /etc/mnttab
swap                                       8460-MB        0-MB     8459-MB          1%  /var/run
swap                                       8513-MB       53-MB     8459-MB          1%  /tmp

$
$
$

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
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Sorting an associative array

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)
Discussion started by: tine
2 Replies

2. Shell Programming and Scripting

Korn Shell and Perl Administrative Script

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)
Discussion started by: metallica1973
0 Replies

3. Shell Programming and Scripting

convert the below perl sript to shell script

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)
Discussion started by: mail2sant
1 Replies

4. Shell Programming and Scripting

Shell/Perl script to convert to Capitalize case

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)
Discussion started by: parshant_bvcoe
11 Replies

5. Shell Programming and Scripting

perl array sorting

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)
Discussion started by: bsdeepu
2 Replies

6. Shell Programming and Scripting

convert perl code to shell script

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)
Discussion started by: x34
2 Replies

7. Shell Programming and Scripting

[Perl] Sorting an String-Array

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)
Discussion started by: buckelede
1 Replies

8. UNIX for Advanced & Expert Users

Calling PERL from a Korn shell script

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)
Discussion started by: mseanowen
2 Replies

9. Shell Programming and Scripting

Convert shell script to Perl

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)
Discussion started by: gvolpini
1 Replies

10. UNIX for Beginners Questions & Answers

Perl/Array Sorting : Can someone please explain below code $h{$_}++

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)
Discussion started by: Tanu
1 Replies
All times are GMT -4. The time now is 04:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy