Solved

Awk question

Posted on 2009-07-13
3
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now