formatting of df -k


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formatting of df -k
# 1  
Old 12-24-2010
Data formatting of df -k

Hello,

I am developing a platform Independant tool that should work for all major unix flavors outlined in this forum(Solaris,Linux, AIX, HPUX, SCO,BSD)

Therefore, in order to cover all types of user community, I have deliberately posted the same message on every forum. Please do not think of this as spamming multiple forums. This is one of a kind request and I do not wish to continue with this approach(of spreading same post to multiple forums)


Now the question:

HTML Code:
The output on df -k on HP looks like below

/r01                   (/dev/vgprj2app/vr01   ) : 207501014 total allocated Kb
                                                  33261948 free allocated Kb
                                                  174239066 used allocated Kb
                                                        83 % allocation used
/tmp                   (/dev/vg00/lvol4       ) : 11798652 total allocated Kb
                                                  11774476 free allocated Kb
                                                     24176 used allocated Kb
                                                         0 % allocation used
/usr                   (/dev/vg00/lvol7       ) :  4960928 total allocated Kb
                                                   2534000 free allocated Kb
                                                   2426928 used allocated Kb
On Solaris and others the same df -k will show the output in a very different format.

I am looking to massage above output to a comma delimited format to stream line the same format that will work as an input to Oracle databases.

HTML Code:
Mount,totalAllocated,FreeAllocated,usedAllocated,percent
"/r01",207501014,33261948,174239066,83
"/tmp",11798652,11774476,24176,0
"/usr",4960928,2534000,2426928,80
How can I massage the df -k ouput formats of various kinds into the one mentioned above using shell scripts? I heard that awk utility can do this, but I just dont know awk programming Smilie

Thanks,
D
# 2  
Old 12-24-2010
If you want to make a truly crossplatform tool, you'd do better to learn the API's instead of processing the df -k output.
# 3  
Old 12-25-2010
I'm no Perl programmer so please be kind. Smilie

I also wanted df output that would work on AIX, HP-UX and Linux. So I run the df command and massage the input so that the output is consistent.

My approach was to write a bit a of code for each OS.

Ex:
Code:
$SYS_NAME=`uname -s`;

if ($SYS_NAME =~ "AIX") {
   $SYS_TYPE=1;
   `/usr/bin/df -k | /usr/bin/sort +6 >$FILE`;
}
elsif ($SYS_NAME =~ "HP-UX") {
   $SYS_TYPE=2;
   `/usr/bin/bdf -i >$FILE`;
}
elsif ($SYS_NAME =~ "Linux") {
   $SYS_TYPE=3;
   `/bin/df -kP >$FILE`;
}
else {
   print "Unknown system: $SYS_NAME.";
   exit -1;
}

Then:
Code:
open (DF, $FILE) || die "Can't Open File: $FILE\n";
while (defined ($_ = <DF> )){

   ###########################################################################
   # AIX
   ###########################################################################
   if ($SYS_TYPE == 1){
      ($FS, $BLOCKS, $FSFREE, $FSUSED, $IUSED, $PIUSED, $MOUNT) = split;
      $FSUSED=~s/([0-9])\%/$1/;
      $PIUSED=~s/([0-9])\%/$1/;

   }


...

   ###########################################################################
   # Every OS
   ###########################################################################
   if ($MOUNT eq "\/") {
      $FS = "Root"
   }
   elsif ($FS=~/:/) {
      $FS1 = "NFS from " . $FS; $FS=$FS1
   }

   if ($FSUSED >= 90)
      $FSUSED = "*$FSUSED*";
   }

   if ($PIUSED > 60) {
      $PIUSED = "*$PIUSED*";
   }

   if ($BLOCKS ne "Size (MB)") { $BLOCKS /= 1024 };
   if ($FSFREE ne "Free (MB)") { $FSFREE = int ($FSFREE /= 1024) };

   if ( (not $MOUNT=~"\/u\/") && (not $MOUNT=~"Mounted") || ($FS=~"NFS") ) {
      printf "%-38s %-9d %-9d %-7s %-7s %-11s\n",$MOUNT, $BLOCKS, $FSFREE, $FSUSED, $PIUSED, $FS;
   }
}

Which results in:

Code:
mi_df
======================================================================================================================================================
Filesystem                             Size (MB) Free (MB) %Used   %Iused  Mount
======================================================================================================================================================
/proc                                  0         0         -       -       /proc
/                                      512       389       24      12      Root
/home                                  8192      4637      44      7       /dev/hd1
/home/dbdumps                          1097728   320906    71      1       NFS from unxr1_sw:/home/dbdumps
/nim_export                            65536     7909      88      4       /dev/nim_lv
/opt                                   2048      742       64      14      /dev/hd10opt
/tftpboot                              128       75        41      1       /dev/fslv00
/tmp                                   256       250       3       1       /dev/hd3
/usr                                   5120      2561      50      11      /dev/hd2
/var                                   1024      540       48      9       /dev/hd9var
======================================================================================================================================================

# 4  
Old 12-25-2010
Error

Thank you purdym for the detail posting. I tried to compile your code as is on my HPUX Box and it gives following error:
HTML Code:
shell.sh" 60 lines, 1454 characters
daahpdev: dev/home/rvaishna$ shell.sh
shell.sh: =HP-UX:  not found.
when I do uname -s from $ prompt it gives me exactly HP-UX. Not sure why it is not working from shell script.

Any idea why this error is coming?

Thanks,
Darsh
# 5  
Old 12-25-2010
Quote:
Originally Posted by darsh123
I tried to compile your code as is on my HPUX Box and it gives following error:
That would be the problem then. You never compile a shell script.

More seriously, that is a perl script, and you seem to be running it in shell...
# 6  
Old 12-25-2010
Thank you for the reply. I actually meant while executing the shell script.

As you have probably figured out by now, I am a novice to shell programming and perl.

Is there anyway I can achieve a CSV(Comma separated values) tabular output of df/bdf command using simply shell programming?

Thanks,
Darsh
# 7  
Old 12-25-2010
So I didn't post the entire complete script on purpose. I was trying to give you and idea of how it could be done.

You can use the same logic in shell scripting. ie. test for what OS your using and run slightly different code for each OS. And hopefully there is some common code.

I will say that it is more complicated but possible to write code to run on multiple OSs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with formatting

Hi, I am new to UNIX and need your help in formatting the below input command to the desire output Input: CREATE UNIQUE INDEX XPKTABLE1 ( COL1, COL2 ) ON TABLE_NM; Output: COMMENT ON TABLE DB_NM.TABLE_NM AS 'PK=,COL1,COL2; '; In... (14 Replies)
Discussion started by: varun2327
14 Replies

2. Shell Programming and Scripting

Formatting

Good day All, I have requirement where my input data looks like below ] Message5 Expecting Output as 04/MAR/2104 ||| 23:15:45 ||| servername ||| NOTIFICATION |||message1||||||userId|||||| Message5 I could not use space delimiter as in the messages I would be having them as... (2 Replies)
Discussion started by: Tomlight
2 Replies

3. Shell Programming and Scripting

Formatting Help

Hi Guys, i have report that runs every 10 min and send the report of failed jobs to my mail. Currently i am using a command like this to send mail. mailx -t -s "FAILURE JOBS IN HYDRA $temp_date" addressee@domain.com < temp_file5 But i am getting mail in this format ....... (4 Replies)
Discussion started by: gkrish
4 Replies

4. Shell Programming and Scripting

help formatting

I need to format a txt file and convert it in CSV. Any "future" column is separated by a newline. FROM: XS1 1.43294 0.0 XS2 1.21824 0.0 TO: XS1,XS2 (2 Replies)
Discussion started by: alfreale
2 Replies

5. Shell Programming and Scripting

Formatting

Is there a way to make a 2 column output out of the following : 1 2 3 4 5 6 Output : 1 2 3 4 5 6 Thanks, Prasanna (3 Replies)
Discussion started by: prasanna1157
3 Replies

6. Shell Programming and Scripting

formatting

I have file with different columns for ex. contents of file "txt" NAME AGE MARKS HARRY 23 89 TOM 12 67 BOB 23 11 and you see its not formatted.Now, I need the file "txt" to be formatted like COLUMN1 COLUMN2 COLUMN3 NAME AGE ... (3 Replies)
Discussion started by: vijay_0209
3 Replies

7. Shell Programming and Scripting

Formatting

I have next part of script: for i in $LIST do echo "`date +"%H:%M:%S"` Converting $i ..."; mysql -uroot some -sABe "ALTER TABLE $i ENGINE=$ENGINE"; done I want to get following output formatting: "OK" must be one under another :) ... (3 Replies)
Discussion started by: mirusnet
3 Replies

8. UNIX for Dummies Questions & Answers

formatting

Hi Again Guys , Please i installed linux RH 6.1 on Toshiba , 10G , RAM=128 , 600 MHZ . After i installed linux i got many error messages , seems it was not installed correctly , also when i finished installation it did not ask me for the 2nd installation CD , and when i logged as root , i... (5 Replies)
Discussion started by: tamemi
5 Replies

9. UNIX for Dummies Questions & Answers

formatting

is it possible to format a powerbook g4 mac? like totally erase the HD then pop in the Mac OS cd and it will boot up an install like windows or any linux? (5 Replies)
Discussion started by: xeron
5 Replies

10. UNIX for Dummies Questions & Answers

formatting

I've been asking on IRC channels but no one answers me, I need to format my hard drive, normally it's just format c: but c doesn't exist, how do I format when I have linux mandrake installed. Please reply to this quickly, I'm kinda in a rush :( (1 Reply)
Discussion started by: darryll777
1 Replies
Login or Register to Ask a Question