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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kilobyte conversion 3

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
This is more of a tool question:

is there a UNIX tool to help me convert Byte, KB, MB, GB?

for example:
tool_name 1024k MB
returns 1.0 MB

tool_name 5128m KB
return 5251072.0(KB)

TIA.
 
As far as I know there isn't a tool, however it wouldn't be very difficult to script a simple function to do this. Pass 2 parameters to it and return the converted value. You never know, if you were to write something that works and post it in this forum, you might get a star or two.

I hope that helps

Mike
 
there's probably a more elegant solution but:

Code:
#!/bin/ksh
# # #
# convert B/K/M/G/T/P
#         byte
#           kilobyte
#             megabyte
#               gigabyte
#                 terabyte
#                   petabyte
# # #
# parameters
value=$1
req_unit=$2
echo ${value} ${req_unit}|\
 tr '[A-Z]' '[a-z]'|\
 awk 'BEGIN {
 B=1
 K=1024
 M=1024*K
 G=1024*M
 T=1024*G
 P=1024*T
}
{
 value_unit=substr($1,length($1),1)
 value_size=substr($1,1,length($1)-1)
 if (value_unit=="k") value_size*=K
 if (value_unit=="m") value_size*=M
 if (value_unit=="g") value_size*=G
 if (value_unit=="t") value_size*=T
 if (value_unit=="p") value_size*=P
 if ((value_unit>="0")&&(value_unit<="9")) value_size=$1
 if ($2=="k") printf "%.1f KB\n", value_size/K
 if ($2=="m") printf "%.1f MB\n", value_size/M
 if ($2=="g") printf "%.1f GB\n", value_size/G
 if ($2=="t") printf "%.1f TB\n", value_size/T
 if ($2=="p") printf "%.1f PB\n", value_size/P
}'

works.

HTH,

p5wizard
 
Hi

With absolutely no check :
Code:
#! /usr/bin/awk -f 

BEGIN {
  split("k m g t p",m)
  m[0]=""
  for (i in m) m[m[i]]=i
  print ARGV[1]*1024^m[ARGV[2]]/1024^m[ARGV[3]]
}

Feherke.
 
feherke:
Ha! very nice! but you forgot the most important one for my purpose, "b"

cool way of doing it though....
 
Hi

Sorry, looks abit obscure indeed.
Syntax :
kilo.awk [value] [from] [to]
Defaults :
value = 0
from, to = '' = bytes
Examples :
kilo.awk 512 m k # 512 Mbytes to Kbytes
kilo.awk 512 k # 512 Kbytes to bytes
kilo.awk 512 m # 512 Mbytes to bytes
kilo.awk 512 '' m # 512 bytes to Mbytes
At least according to my poor mathematic knowledges.

Feherke.
 
no doubt very clever!!!

But what about the byte to kilobyte part?

Still a star deserved :)
 
Hi

Mag0007 said:
But what about the byte to kilobyte part?
'' ( nothing ) = bytes, k = kilobytes, m = megabytes ...
So, if you want to find out for example 2005 bytes how many kilobytes are, then :
Code:
[blue]master #[/blue] kilo.awk 2005 '' k
1.95801

The same script abit modified. The [highlight #eeeeff][tt]m[0]=""[/tt][/highlight] is needless, and the output could be nicer.
Code:
#! /usr/bin/awk -f

BEGIN {
  split("k m g t p e z y",m)
  for (i in m) m[m[i]]=i
  print ARGV[1] " " ARGV[2] "b = " ARGV[1]*1024^m[ARGV[2]]/1024^m[ARGV[3]] " " ARGV[3] "b"
}
Code:
[blue]master #[/blue] kilo.awk 2005 '' k
2005 b = 1.95801 kb

Feherke.
 
Here's a version with no "split" and no "for" loop:
Code:
function b(u) { return 1024^index("kmgtpezy",u) }
BEGIN {
  print ARGV[1],ARGV[2] " = " ARGV[1]*b(ARGV[2]) / b(ARGV[3]),
    ARGV[3]
}
[tt]awk -f bytes.awk 144000 b k
144000 b = 140.625 k
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top