LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-03-2011, 01:12 AM   #1
ksushants
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Rep: Reputation: 0
Awk: try to find space used


Hi All,

I have a code line that finds the space consumed by the directory datadir:

df $datadir | grep "/" | sort -u | awk '{printf("%i\n", 100-$4/$2*100)}'

When the space consumption for this directory is above 10% is gives proper output, but if less than 10% it gives wrong values:

The space consumption on 1 of my servers is 7% of that directory, it gives following error:

99
awk: (FILENAME=- FNR=2) fatal: division by zero attempted

Can you please let me know what is the issue with the AWK and what is the correct code.

Thanks in advance.
Regards,
Sushant
 
Old 10-03-2011, 01:45 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

I am not sure if awk is really the problem here. df tries to keep the columns aligned by default. Sometimes this leads to output that spans over two lines, e.g. when the device name is very long. Then the fields in awk are not always referencing the same columns.
Try this:
Code:
df -P $datadir | tail -1 | awk '{printf("%i\n", 100-$4/$2*100)}'
BTW, why aren't you just using the 'used' value of df in coulumn $5?
 
1 members found this post helpful.
Old 10-04-2011, 01:12 AM   #3
murugesan
Member
 
Registered: May 2003
Posts: 155

Rep: Reputation: 28
Updated code:

Code:
#!/bin/ksh
datadir="$1"
echo output of the command \"df $datadir\"
df $datadir
echo output using grep sort awk:
df $datadir | grep "/" | sort -u | awk '{
        if ( $2 > 0 )
        {
                printf("%i\n", 100-$4/$2*100)
        }
        else
        {
                printf("%i\n", 100-$4/100)
        }
}'
 
Old 10-04-2011, 01:25 AM   #4
murugesan
Member
 
Registered: May 2003
Posts: 155

Rep: Reputation: 28
Script with exception handling

Code:
#!/bin/ksh
if [ $# -eq 0 ]
then
        echo Usage:
        echo $0 any_Dir_or_File_Location
        return 1
fi
if [ -f $@ ] || [ -d $@ ]
then
        datadir="$1"
        echo output of the command \"df $datadir\"
        df $datadir
        echo output using grep sort awk:
        df $datadir | grep "/" | sort -u | awk '{
                if ( $2 > 0 )
                {
                        printf("%i\n", 100-$4/$2*100)
                }
                else
                {
                        printf("%i\n", 100-$4/100)
                }
        }'
fi
 
Old 10-04-2011, 06:58 PM   #5
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Linux Mint, Ubuntu
Posts: 1,600

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Code:
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda5       80634688 27763604  48775084  37% /mnt/Common
Note: Used + Available ≠ 1K-blocks. According to my calculations, 27763604/80634688=34.4%; 48775084/80634688=60.5%; 34.4%+60.5%=94.9%. I believe the system reserves 5% for root. FWIW, OP's calculation apparently fails to take that 5% into account.
 
Old 10-05-2011, 08:58 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

I see that you marked the thread as [SOLVED]. Glad, you got it fixed. One more thing I want to add is that I realize that
$datadir could contain more than one path of directories. Not sure if this is a possible scenario in your script but here is a command that will also handle this case:
Code:
df -P $datadir | awk '(NR != 1) {printf("%i\n", 100-$4/$2*100)}'
This way you also only need one pipe.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find URL in Debian package index via awk/sed (=find a line, then search from there) maddes.b Linux - Software 11 06-28-2013 08:37 AM
[SOLVED] Round disk space with awk vanish78 Linux - Newbie 6 06-03-2011 07:10 AM
awk parse string with space? yech Programming 8 02-12-2010 02:33 AM
How to remove everything before the first space in Sed or Awk OutThere Linux - General 1 04-05-2009 11:45 PM
awk - working with space and tab indiancosmonaut Programming 7 07-10-2007 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration