convert file storage size from one unit of measurement to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert file storage size from one unit of measurement to another
# 1  
Old 03-15-2009
convert file storage size from one unit of measurement to another

The source number is always in megabytes and I need a script to covert into either MB,GB or TB displayed in a specific format.

Source number examples:

5345376635
34255
5453645846353

The result has to be maximum 2 numbers after the comma

1.13 TB
134.17 TB
413.46 GB
678.45 MB

I know a couple ways to calculate it but how to make it just 2 numbers after comma?

Code:
 EXAMPLE1="`expr $SOURCE / 1024 / 1024` TB"
 EXAMPLE2="$(($SOURCE/1024/1024)) TB"
 EXAMPLE3="`echo|awk '{print '$SOURCE' / 1024 / 1024}'` TB"

# 2  
Old 03-15-2009
Use printf to format the output:

Code:
printf "%.2f\n" $SOURCE

Regards
# 3  
Old 03-15-2009
Quote:
Originally Posted by Franklin52
Use printf to format the output:

Code:
printf "%.2f\n" $SOURCE

Regards
Thanks that does solve my main problem but one more question..

Code:
TEST="`echo|awk '{print 7068099 / 1024 / 1024}' | printf "%.2f\n"` TB"
echo $TEST

How can I make it work like this?

I've got the following alternative solution but that's one unnecessary step!?
Code:
TEST=`echo|awk '{print 7068099 / 1024 / 1024}'
TEST2="`printf "%.2f\n" $TEST` TB"
echo $TEST2


Last edited by TehOne; 03-15-2009 at 11:31 AM..
# 4  
Old 03-15-2009
Should be something like:

Code:
TEST=$(awk 'BEGIN{printf "%.2f\n",  7068099 / 1024 / 1024}')

Or as a variable:

Code:
TEST=$(awk -v a="7068099" 'BEGIN{printf "%.2f\n",  a/1024/1024}')

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Shell Programming and Scripting

Measurement file parsing

I have an application performance measurement file with one thousand lines. Each line has some text indicating type of measurement and the last field containing the measured value. Each of the file has a unique measurement. I am interested in only extracting about 100 of those measurements and put... (2 Replies)
Discussion started by: yoda9691
2 Replies

3. UNIX for Dummies Questions & Answers

Display storage type and storage size

Can anybody help me ont to get the command to check storage type and storage size in unix. Thank u in advanced!! (4 Replies)
Discussion started by: manisham
4 Replies

4. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

5. AIX

VIO SAN STORAGE LPAR ( Dynamically increasing size )

Hi, Is there a way to dynamically increase the size of virtual disk on the LPAR. The virtual disk is coming from my VIO Server. From my SAN I have allocated a disk to VIO Server and from VIO Server to my LPAR....If I increase the space of the logical SAN DISK (DS 4700 using IBM TotalStorage... (0 Replies)
Discussion started by: filosophizer
0 Replies

6. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

7. Filesystems, Disks and Memory

AIX and DS4300 Storage Unit

Hello, We're setting up a solution for a group of customers with 2 p520 servers and 1 DS4300 unit with 9 disks (at this stage). The meaning is to create two arrays on the DS4300. Both servers will be connected to the DS4300 unit and to both controllers (e.g. Controller 1 connected to server 1... (0 Replies)
Discussion started by: EricBE
0 Replies

8. UNIX for Dummies Questions & Answers

Convert block size to mb

If I execute this command: $ ls -lt | awk '{print $5}' | sort -nr |head -1 it returns the following value 57441881 If I execute this command: $ ls -s | sort -nr | head -1 | cut -d" " -f1 it returns the same file but now in block size 112208 Is there... (1 Reply)
Discussion started by: mh53j_fe
1 Replies
Login or Register to Ask a Question