numfmt 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

numfmt 命令安装:


-bash/zsh: numfmt command not found

#Debian
apt-get install coreutils

#Ubuntu
apt-get install coreutils

#Alpine
apk add coreutils

#Arch Linux
pacman -S coreutils

#Kali Linux
apt-get install coreutils

#CentOS
yum install coreutils

#Fedora
dnf install coreutils

#OS X
brew install coreutils

#Raspbian
apt-get install coreutils

#Docker
docker run cmd.cat/numfmt numfmt

numfmt 命令补充说明:


numfmt 命令用来实现数字和易读形式之间的转换。numfmt 默认是 coreutils 的一部分,默认情况下无需另外安装。

numfmt 命令语法:


numfmt [OPTION]... [NUMBER]...

numfmt 命令选项:


Reformat NUMBER(s), or the numbers from standard input if none are specified.

Mandatory arguments to long options are mandatory for short options too.

--debug
      print warnings about invalid input

-d, --delimiter=X
      use X instead of whitespace for field delimiter

--field=N
      replace the number in input field N (default is 1)

--format=FORMAT
      use printf style floating-point FORMAT; see FORMAT below for details

--from=UNIT
      auto-scale input numbers to UNITs; default is 'none'; see UNIT below

--from-unit=N
      specify the input unit size (instead of the default 1)

--grouping
      use locale-defined grouping of digits, e.g. 1,000,000 (which means it has no effect
      in the C/POSIX locale)

--header[=N]
      print (without converting) the first N  header  lines;  N  defaults  to  1  if  not
      specified

--invalid=MODE
      failure mode for invalid numbers: MODE can be: abort (default), fail, warn, ignore

--padding=N
      pad  the  output  to  N  characters;  positive  N will right-align; negative N will
      left-align; padding is ignored if the output is wider than N;  the  default  is  to
      automatically pad if a whitespace is found

--round=METHOD
      use METHOD for rounding when scaling; METHOD can be: up, down, from-zero (default),
      towards-zero, nearest

--suffix=SUFFIX
      add SUFFIX to output numbers, and accept optional SUFFIX in input numbers

--to=UNIT
      auto-scale output numbers to UNITs; see UNIT below

--to-unit=N
      the output unit size (instead of the default 1)

--help display this help and exit

--version
      output version information and exit

UNIT options:
none   no auto-scaling is done; suffixes will trigger an error

auto   accept optional single/two letter suffix:

      1K = 1000, 1Ki = 1024, 1M = 1000000, 1Mi = 1048576,

si     accept optional single letter suffix:

      1K = 1000, 1M = 1000000, ...

iec    accept optional single letter suffix:

      1K = 1024, 1M = 1048576, ...

iec-i  accept optional two-letter suffix:

      1Ki = 1024, 1Mi = 1048576, ...

FORMAT must be suitable for printing one floating-point  argument  '%f'.   Optional  quote
(%'f)  will  enable  --grouping  (if  supported  by current locale).  Optional width value
(%10f) will pad output. Optional negative width values (%-10f) will left-pad output.

Exit status is 0 if all input numbers were successfully  converted.   By  default,  numfmt
will  stop  at  the  first  conversion  error with exit status 2.  With --invalid='fail' a
warning  is  printed  for  each  conversion  error  and  the  exit  status  is  2.    With
--invalid='warn'  each  conversion  error  is  diagnosed,  but the exit status is 0.  With
--invalid='ignore' conversion errors are not diagnosed and the exit status is 0.

numfmt 命令参数:


待转换的数字

numfmt 命令实例:


numfmt 将 1.5K(国际单位制)转换为 1500:

numfmt --from=si 1.5K

1500

numfmt 将第 5 个字段(1索引)转换为 IEC 单位,而无需转换标题:

ls -l | numfmt --header=1 --field=5 --to=iec

total 28
-rw-r--r-- 1 root root   25K Apr  7  2017 mysql57-community-release-el7-10.noarch.rpm

numfmt 转换为 IEC 单位,填充 5 个字符,左对齐:

du -s * | numfmt --to=iec --format="%-5f"

28    mysql57-community-release-el7-10.noarch.rpm

numfmt 其他示例:

$ numfmt --to=si 1000

-> "1.0K"


$ numfmt --to=iec 2048

-> "2.0K"


$ numfmt --to=iec-i 4096

-> "4.0Ki"


$ echo 1K | numfmt --from=si

-> "1000"


$ echo 1K | numfmt --from=iec

-> "1024"

$ df | numfmt --header --field 2 --to=si
$ ls -l | numfmt --header --field 5 --to=iec
$ ls -lh | numfmt --header --field 5 --from=iec --padding=10
$ ls -lh | numfmt --header --field 5 --from=iec --format %10f

numfmt 命令扩展阅读:




numfmt 命令评论