Awk question


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;


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


# 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

Red-KingIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Using a repeated 'df', no heavy scripting necessary:

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
    df -k $fs 2>/dev/null | grep -v "Filesystem" | awk '{printf "%4s %-12s\n", $4, $7}'
   done ) | sendmail -t 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Red-KingIT ManagerAuthor Commented:
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,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.