Solved

Awk question

Posted on 2009-07-13
3
370 Views
Last Modified: 2013-11-17
Hi,

I'm running the following in a shell script to pull specific information from the df output.
The script itself works fine and I get the information I want.

One small thing I'd like to change is the order the information is output in.
df -k gives the following output;

Filesystem    1024-blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4          1048576    747604   29%     2510     1% /
/dev/hd2          2621440    796848   70%    36959     6% /usr
/dev/hd9var        524288     98592   82%     2127     2% /var
/dev/hd3           262144    162772   38%      509     1% /tmp
/dev/hd1           262144    253708    4%       69     1% /home
/proc                   -         -    -         -     -  /proc
/dev/hd10opt       262144    242048    8%      491     1% /opt
/dev/root          262144    149540   43%      264     1% /root
/dev/install      1048576    984884    7%       50     1% /usr/sys/inst.images
/dev/software     9437184   3142060   67%    23096     1% /software
/dev/manuals       262144    221824   16%     2090     4% /usr/share/man
/dev/usr2         1572864    985288   38%    10895     3% /usr2
/dev/ni_progs      786432    286072   64%    14662     8% /usr2/basic/NI/PROGRAMS
/dev/ni_data     10223616   2012168   81%     7139     1% /usr2/basic/NI/DATA
/dev/live_progs      786432    156084   81%     8160     5% /usr2/basic/LIVE/PROGRAMS
/dev/live_data    83623936  31105552   63%   242182     3% /usr2/basic/LIVE/DATA
/dev/live_data2    10223616   8249528   20%       57     1% /usr2/basic/LIVE/DATA2
/dev/test_data    83623936  18627700   78%    10833     1% /usr2/basic/TEST/DATA
/dev/test_progs      786432    498176   37%     7293     4% /usr2/basic/TEST/PROGRAMS
/dev/sess        10223616   9770280    5%     1352     1% /usr2/basic/SESS
/dev/emails       5242880   2242688   58%   436758    34% /usr2/basic/EMAIL

My awk command pulls out this;

TO: <snip>
SUBJECT:Disk Utilisation

Used    Partition
82%     /var
38%     /tmp
38%     /usr2
58%     /usr2/basic/EMAIL

What I'd like to do is change the order that the matches are out put in without having to run the df more than once. The order I'm trying to get is;

/usr2
/tmp
/var
/usr2/basic/EMAIL

Is there an easy way to do this or will I need to script this a bit more heavily?

Rory

#!/bin/ksh
# This script will get the disk utilisation of key partitions and use sendmail to send .
 
df -k | awk 'BEGIN {print "TO:<SNIP>\nSUBJECT:Disk Utilisation\n\nUsed\tPartition"} /usr2$/ {print $4"\t"$7} /tmp/ {print $4"\t"$7} /var/ {print $4"\t"$7} /EMAIL/ {print $4"\t"$7}' |sendmail -t

Open in new window

0
Comment
Question by:Red-King
  • 2
3 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24838330
Hi,
unfortunately you won't be able to achieve that without repeating the df.  
Your sort criteria is arbitrary, meaning not related to the content of the strings to be sorted, and to nothing else in the output of 'df', as far as I can see.
Sorry, no way.
wmp
 
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 125 total points
ID: 24838515
Using a repeated 'df', no heavy scripting necessary:

#!/bin/ksh
filesystems="/var /tmp /usr2 /usr2/basic/EMAIL"
( printf "%s\n%s\n%4s %-12s\n" "TO:<snip>" "Subject: Disk Utilization" "Used" "Partition"
  echo $filesystems | while read fs
   do
    df -k $fs 2>/dev/null | grep -v "Filesystem" | awk '{printf "%4s %-12s\n", $4, $7}'
   done ) | sendmail -t 

Open in new window

0
 
LVL 9

Author Comment

by:Red-King
ID: 24838647
Thanks for the reply WMP.
Your script ran first time too, nice work.
I know four df's and awk's won't have any real impact on performance, but I like to make sure I'm being as efficent as possible by principle.

Thanks again,
Rory
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question