?
Solved

Awk question

Posted on 2009-07-13
3
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project
Suggested Courses

752 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