coreutils
[Top][All Lists]
Advanced

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

Re: Command-line program to convert 'human' sizes?


From: Assaf Gordon
Subject: Re: Command-line program to convert 'human' sizes?
Date: Tue, 04 Dec 2012 17:55:57 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120510 Icedove/10.0.4

Hello,

> Pádraig Brady wrote, On 12/04/2012 11:30 AM:
>> Nothing yet. The plan is to make a numfmt command available with this 
>> interface:
>> http://lists.gnu.org/archive/html/coreutils/2012-02/msg00085.html
>>

Attached is a patch, with a proof-of-concept working 'numfmt'.

Works: from=SI/IEC/AUTO, to=SI/IEC, from-units, to-units, suffix, round.
Doesn't work: format, to=<NUMBER>,field=N .

The code isn't clean and can be improved.
Currently, either  every (non-option) command-line parameter is expected to be 
a number, or every line on stdin is expected to start with a number.
 
Comments are welcomed,
 -gordon


Examples;
====
$ ./src/numfmt --from=auto 2K
2000
$ ./src/numfmt --from=auto 2Ki
2048
$ ./src/numfmt --from=SI 2K    
2000
$ ./src/numfmt --from=SI 2Ki
2000
$ ./src/numfmt --from=IEC  2Ki
2048
$ ./src/numfmt --from=SI --to=IEC 2Ki
2.0K
$ ./src/numfmt --from=IEC --to=SI 2K 
2.1k
$ ./src/numfmt --from=IEC 1M        
1048576
$ ./src/numfmt --from=IEC --to=SI 1M
1.1M
$ ./src/numfmt --from=IEC --to-unit=20 1M
52429
./src/numfmt --from-unit=512 --to=IEC 4
2.0K
$ ./src/numfmt --round=ceiling --to=IEC 2000
2.0K
$ ./src/numfmt --round=floor --to=IEC 2000
1.9K
====

Help screen
===
$ ./src/numfmt --help 
Usage: ./src/numfmt [OPTIONS] [NUMBER]
Reformats NUMBER(s) to/from human-readable values.
Numbers can be processed either from stdin or command arguments.

  --from=UNIT     Auto-scale input numbers (auto, SI, IEC)
                  If not specified, input suffixed are ignored.
  --from-unit=N   Specifiy the input unit size (instead of the default 1).
  --to=UNIT       Auto-scale output numbres (SI,IEC,<N>).
                  If not specified, XXXX
  --to-unit=N     Specifiy the output unit size (instead of the default 1).
  --rount=METHOD  Round input numbers. METHOD can be:
                  ceiling (the default), floor, nearest
  -f, --format=FORMAT   use printf style output FORMAT.
                        Default output format is %d .
  --suffix=SUFFIX       XXXX
  
      --help     display this help and exit
      --version  output version information and exit

UNIT options:
 auto ('--from' only):
      1K  = 1000
      1Ki = 1024
      1G  = 1000000
      1Gi = 1048576
 SI:
      1K* = 1000
      (additional suffixes after K/G/T do not alter the scale)
 IEC:
      1K* = 1024
      (additional suffixes after K/G/T do not alter the scale)
 <N> ('--to' only):
      Use number N as the scale.


Examples:
  ./src/numfmt --to=SI 1000           -> "1K"
  echo 1K | ./src/numfmt --from=SI    -> "1000"
  echo 1K | ./src/numfmt --from=IEC   -> "1024"

Report numfmt bugs to address@hidden
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report numfmt translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'numfmt invocation'
===

Attachment: numfmt1.patch
Description: Text Data


reply via email to

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