coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding humanize_number to coreutiles?


From: Peng Yu
Subject: Re: Adding humanize_number to coreutiles?
Date: Sun, 12 Feb 2012 21:45:26 -0600

2012/2/7 Pádraig Brady <address@hidden>:
> On 02/07/2012 03:36 AM, Peng Yu wrote:
>> Hi,
>>
>> Several commands in coreutils have the -h option. I'm wondering
>> whether anybody in the develop team also thinks that it is worthwhile
>> to export it as a standalone command. If so, I'd recommend add such
>> convenient command in coreutiles. As I don't find it anywhere else as
>> a stand alone command.
>>
>> http://siarzhuk.dyndns.org/haiku/doxygen/coreutils_2lib_2human_8c_source.html#l00154
>
> I've needed such functionality many times.
> I'm thinking a printf format would be best to expose this:
> http://lists.gnu.org/archive/html/coreutils/2011-08/msg00029.html
>
> %H seems like it might cause compat problems in future.
> %{human} is more descriptive and extensible, so I'm leaning towards that.
> Any other suggestions appreciated.
>
> I'll work on it this week.

I'm not sure %{human} is enough for configuring all the possible ways
of printing a humanized number. See my code below, there are binary
and decimal humanized numbers. Also, you'd better allow a space (or
not) between the numbers and the letters (such as 'T', 'G').

Also embedding it in printf will make it hard to be found, I'd
recommend to create a new command like humanizenumber, just as I did.

/tmp$ cat `which humanizenumber.sh `
#!/usr/bin/env bash

script_name=`basename "$0" .sh`

TEMP=`getopt -o hbd:sn --long
help,binary,number_of_decimal_places:,space,newline -n
"${script_name}.sh" -- "$@"`

if [ $? != 0 ] ; then printf "Terminating...\n" >&2 ; exit 1 ; fi

eval set -- "$TEMP"

abspath_script=`readlink -f -e "$0"`
script_absdir=`dirname "$abspath_script"`

number_of_decimal_places=0
while true ; do
  case "$1" in
    -h|--help)
      cat "$script_absdir"/${script_name}_help.txt
      exit
      ;;
    -b|--binary)
      binary=x
      shift
      ;;
    -d|--number_of_decimal_places)
      number_of_decimal_places="$2"
      shift 2
      ;;
    -s|--space)
      space=' '
      shift
      ;;
    -n|--newline)
      newline='\n'
      shift
      ;;
    --)
      shift
      break
      ;;
    *)
      printf "Internal error!\n">&2
      exit 1
      ;;
  esac
done

if [ $# -ne 0 ]
then
  n="$1"
  if [ -n "$binary" ]
  then
    awk -v sum=$n \
      -v space="$space" \
      -v newline="$newline" \
      -v number_of_decimal_places=$number_of_decimal_places '
    BEGIN{
    hum[1024**5]="P"
    hum[1024**4]="T"
    hum[1024**3]="G"
    hum[1024**2]="M"
    hum[1024]="K"
    for (x=1024**5; x>=1024; x/=1024) {
      if (sum>=x) {
        printf "%." number_of_decimal_places "f" space "%s" newline,
sum/x, hum[x]
        break
      }
    }
  }'
else
  awk -v sum=$n \
    -v space="$space" \
    -v newline="$newline" \
    -v number_of_decimal_places=$number_of_decimal_places '
  BEGIN{
  hum[1000**5]="P"
  hum[1000**4]="T"
  hum[1000**3]="G"
  hum[1000**2]="M"
  hum[1000]="k"
  for (x=1000**5; x>=1000; x/=1000) {
    if (sum>=x) {
      printf "%." number_of_decimal_places "f" space "%s" newline, sum/x, hum[x]
      break
    }
  }
}'
  fi
fi
/tmp$  humanizenumber.sh -h
Description:
  Humanize number(s)

Usage:
  humanizenumber.sh [Options] [NUMBER]

    NUMBER                            If not specifed, then do nothing.

Options:
  -h|--help                           Help message.
  -b|--binary                         Default: decimal.
  -s|--space                          Default: nospace.

Examples:
  humanizenumber.sh 456456456
  humanizenumber.sh -d 2 456456456
  humanizenumber.sh -s 456456456
  humanizenumber.sh -b 456456456
  humanizenumber.sh -n 456456456

Author:
  Peng Yu <address@hidden>



-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]