Find the answer to your Linux question:
Results 1 to 4 of 4
Hi everyone !! How can i convert an output in Human Readable size to KB ? I have seen many examples in the opposite but none in order to transform ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6

    Human readable to KB


    Hi everyone !!

    How can i convert an output in Human Readable size to KB ?
    I have seen many examples in the opposite but none in order to transform (Mb, Gb, etc ) to KB

    Thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Are you using a specific program that outputs size, and you want to pass some flag to do this?

    Or are you saying, you have a number (in MB, GB, etc.) and you manually want to convert it?

    If the former, it depends - what is the prog? If the later (which is what i think you mean), do something like this:

    Code:
    #!/bin/bash
    [ $# -ne 2 ] && echo "Usage: <NUMBER> <MB|GB>" && exit 1
    num=$1
    unit=$2
    if [ $unit == 'MB' ]; then
      kb=$(printf '%.0f' $(echo $num \* 1024|bc -l))
      echo $num $unit = $kb KB
    elif [ "$unit" == 'GB' ]; then
      kb=$(printf '%.0f' $(echo $num \* 1048576|bc -l))
      echo $num $unit = $kb KB
    else
      echo "unit \`$unit' unsupported"
      exit 1
    fi

  3. #3
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6
    i can filter some commands to get this output

    34m
    1078
    345m
    4845
    50m
    1,2g
    201m
    2045

    default is kb. if the value is scalated, the letter m or g is added.
    I want all in KB

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, so try this: a single number is passed to this script (any one from the filtered list you provided), and it is checked to figure out what unit it is in. then it is passed to the original code (slightly modified) which is now a function.

    Code:
    #!/bin/bash
    [ $# -ne 1 ] && echo "Usage: $0 <input>" && exit 1
    input=$1
    
    # mb
    echo $input|grep -qi m$
    if [ $? -eq 0 ]; then
      num=$(echo $input|sed -e 's|m$||i')
      unit='MB'
    else
      # gb
      echo $input|grep -qi g$
      if [ $? -eq 0 ]; then
        num=$(echo $input|sed -e 's|g$||i')
        unit='GB'
      else
        echo $input|grep -q ^[0-9]*$
        if [ $? -ne 0 ]; then
          echo "Unexpected input \`$input'"
          exit 1
        fi
        # kb
        num=$input
        unit='KB'
      fi
    fi
    
    function convert_to_kb() {
      [ $# -ne 2 ] && echo "Usage: <NUMBER> <MB|GB>" && return 1
      num=$1
      unit=$2
      if [ $unit == 'MB' ]; then
        kb=$(printf '%.0f' $(echo $num \* 1024|bc -l))
        echo $num $unit = $kb KB
      elif [ "$unit" == 'GB' ]; then
        kb=$(printf '%.0f' $(echo $num \* 1048576|bc -l))
        echo $num $unit = $kb KB
      else
        echo "unit \`$unit' unsupported" >&2
        return 1
      fi
    }
    
    convert_to_kb $num $unit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •