Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP CALCULATING THE TOTAL NUMBER OF BYTES 2

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have a file which contains node names and the number of bytes backed up. I would like to calculate the total number of bytes and add it to the output file. A sample of the output is shown below. The values are in bytes, KB, MB, and GB. What would be the best way to go about doing this? Any help would be greatly appreciated.

NODE1 1.02 GB
NODE2 320.55 MB
NODE3 0 B

Thanks,

John
 
Try this ( untested):
awk '{BEGIN tot=0}
{
if($3=="GB")
tot+=$2*1000
else
if($3=="MB")
tot+=$2*1000000
else
tot+=$2
}
END{
print tot
}' node_out_file

HTH Dickie Bird (:)-)))
 
Thanks for the challenge, here is my solution.
[tt]
awk '{ if ($3=="GB") {bytes+=$2*1024*1024*1024}
else if ($3=="MB") {bytes+=$2*1024*1024}
else if ($3=="KB") {bytes+=$2*1024}
else if ($3=="B") {bytes+=$2}
} END {printf "Total %d B\n", bytes}' file1
[/tt]
Tested...
[tt]
Total 1431337697 B
[/tt]
 
Thanks Ygor and Dickiebird!! With your help I was able to do exactly what I needed to do!! I actually decided to convert to GB since there is so much data (641 GB). Below is what I used:

awk '{ if ($3=="GB") {bytes+=$2}
else if ($3=="MB") {bytes+=$2/1024}
else if ($3=="KB") {bytes+=$2/1024/1024}
else if ($3=="B") {bytes+=$2/1024/1024/1024}
} END {printf "Total:\t %d GB\n", bytes}' file

Thanks,

John

 
#!/usr/bin/ksh
# disk_use.ksh
# calculates disk usage in current directory
# or specified directory
# example: disk_use.ksh or disk_use.ksh /tmp
# displays disk use in byes, kb, MB and GB

################################################################################
# Variables
################################################################################
TMP_LIST=/tmp/TMP_LIST.disk_space.$$ # generic tmp file for data

################################################################################
# Functions
################################################################################
Find_Files ()
{
B_TOTAL=0
find $1 -type f >> $TMP_LIST.find
cat $TMP_LIST.find|while read LINE
do
FILE=$LINE
K=`ls -ld $FILE|awk '{print $5}'`
echo "K = $K"
B_TOTAL=`expr $B_TOTAL + $K`
done
}

Calculate () # peform calculations that require or end in decimals
# the result is returned in the variable $ANSWER
# usage: Calculate [operation] [dec positions]
# ex: Calculate ((144*6)/32) 2
# ex: Calculate ((${NUMBER}*6)/32) 4
{
OPERATION=$1
DEC=$2
> $TMP_LIST.bc
echo "scale=${DEC}" >> $TMP_LIST.bc
echo "$OPERATION" >> $TMP_LIST.bc
echo "quit" >> $TMP_LIST.bc
ANSWER=`bc $TMP_LIST.bc`
}

Disk_Usage ()
{
KB_TOTAL=`du -sk $DIR|awk '{print $1}'`
}

Free_Space ()
{
echo
}

################################################################################
# Main
################################################################################

DIR="$*"
Disk_Usage

Calculate ${KB_TOTAL}*1000 2
B_TOTAL=$ANSWER

Calculate ${KB_TOTAL}/1000 2
MB_TOTAL=$ANSWER

Calculate ${KB_TOTAL}/1000000 2
GB_TOTAL=$ANSWER

echo
echo "bytes: $B_TOTAL"
echo " KB: $KB_TOTAL"
echo " MB: $MB_TOTAL"
echo " GB: $GB_TOTAL"
echo

################################################################################
# End
################################################################################ Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top