当前位置: 首页 > news >正文

【busybox记录】【shell指令】numfmt

目录

内容来源:

【GUN】【numfmt】指令介绍

简介

通用选项

可能的unit

【busybox】【numfmt】指令介绍

【linux】【numfmt】指令介绍

使用示例:

将单个数字 / 转换为人类表示:

从' SI '转换到' IEC '刻度

指定输入和输出刻度后,支持定义的最大前缀:

从输入文件/管道输入转换单个字段

输出可以使用 --padding或 --format来调整:

对于支持分组数字的区域设置,

常用组合指令:

指令不常用/组合用法还需继续挖掘:


内容来源:

        GUN : Coreutils - GNU core utilities

        busybox v1.36.1 : 【busybox记录】【shell指令】基于的Busybox的版本和下载方式-CSDN博客

【GUN】【numfmt】指令介绍

简介

numfmt:重新格式化数字
numfmt 读取各种表示形式的数字,并根据需要重新格式化它们。最常见的用法是将数字与人类表示进行转换(例如' 4G ' → ' 4,000,000,000 ')。
numfmt [option]... [number]
numfmt 根据指定的选项转换命令行上的每个数字(见下文)。
如果没有给出数字,则从标准输入读取数字。
numfmt 可以选择从特定列中提取数字,保持适当的行填充和对齐。退出状态为0表示成功,非0表示失败。
有关退出状态的其他信息,请参阅 --invalid。

通用选项

程序接受以下选项。请参见第2章[常用选项],第2页。
‘--debug’打印(到标准错误)关于可能错误使用的警告消息。
‘-d d’
‘--delimiter=d’使用字符d作为输入字段分隔符(默认为空白)。使用非默认分隔符将关闭自动填充。
‘--field=fields’转换输入字段字段中的数字(默认值:1)。fields支持裁剪样式字段范围:N    第N个字段,从1开始数N-   从第N个字段,到行尾N-M  从N到M的域(含)-M   从第一个字段到第M个字段(含)-    所有字段
‘--format=format’使用print样式的浮动格式字符串。格式字符串必须包含一个' %f '指令,可选地使用‘'’, ‘-’, ‘0’,宽度或精度修饰符。‘'’ 修饰符将启用 --grouping, '-' 修饰符将启用左对齐的 --padding, width修饰符将启用右对齐的 --padding。‘0’ 宽度修饰符(不带'-'修饰符)将在数字上生成前导零,直至指定的宽度。像“%.1f”这样的精度规范将覆盖从输入数据确定的精度或由于 --to选项自动缩放而设置的精度。
‘--from=unit’根据单位自动缩放输入数字。参见下面的单位。默认值是无缩放,这意味着后缀(例如' M ', ' G ')将触发错误。
‘--from-unit=n’指定输入单位大小(而不是默认的1)。当输入数字表示其他单位时使用此选项(例如,如果输入数字' 10 '表示512字节的10个单位,则使用' --from-unit=512 ')。后缀用' --from=auto '来处理。
‘--grouping’根据当前地区的分组规则对输出数字中的数字进行分组(例如,千位分隔符,通常为‘.’(点)或','(逗号)。此选项在' POSIX/C '语言环境中不起作用。
‘--header[=n]’打印前n行(默认为1行),不进行任何转换。
‘--invalid=mode’输入错误的默认操作是立即退出,状态码为2。--invalid=‘abort’ 显式指定此默认模式。使用' fail '模式,为每个转换错误打印警告,并以状态2退出。使用' warn '模式时,即使存在转换错误,也以状态0退出;使用' ignore '模式时,甚至不打印诊断信息。
‘--padding=n’通过添加空格,将输出数字填充为n个字符。如果n是正数,则数字将向右对齐。如果n是负数,数字将左对齐。默认情况下,数字根据输入行的宽度自动对齐(仅使用默认分隔符)。
‘--round=method’在转换数字表示时,根据方法将数字四舍五入,可以是‘up’, ‘down’, ‘from-zero’ (默认), ‘towards-zero’‘nearest’。
‘--suffix=suffix’在输出数字中添加' SUFFIX ',并在输入数字中接受可选的' SUFFIX '。
‘--to=unit’根据单位自动缩放输出数字。参见下面的单位。默认值是无缩放,这意味着数字的所有数字都被打印出来。
‘--to-unit=n’指定输出单位大小(而不是默认的1)。当输出数字表示其他单位时使用此选项(例如,要在1kB块中表示' 4,000,000 '字节,使用‘--to=si --to-unit=1000’ )。后缀用' --from=auto '来处理。
‘-z’
‘--zero-terminated’用零字节而不是换行符(ASCII LF)分隔项。即,将输入视为由ASCII NUL分隔的项,并以ASCII NUL终止输出项。这个选项可以与' perl -0 '或' find -print0 '和' xargs -0 '结合使用,它们做同样的事情,以便可靠地处理任意文件名(甚至那些包含空格或其他特殊字符的文件名)。使用-z时,换行符被视为字段分隔符。

可能的unit

下面是使用 --from=UNITS 和 --to=UNITS 的可能的单位选项:
none不执行缩放。对于输入数字,不接受后缀,数字后面的任何尾随字符都将触发错误。对于输出数字,将打印数字的所有数字。
si根据国际单位制(SI)标准自动缩放数字。对于输入数字,接受以下后缀之一。对于输出数字,大于1000的值将被舍入,并使用以下后缀之一打印:‘K’ => 10001 = 103 (Kilo) (uppercase accepted on input)‘k’ => 10001 = 103 (Kilo) (lowercase used on output)‘M’ => 10002 = 106 (Mega)‘G’ => 10003 = 109 (Giga)‘T’ => 10004 = 1012 (Tera)‘P’ => 10005 = 1015 (Peta)‘E’ => 10006 = 1018 (Exa)‘Z’ => 10007 = 1021 (Zetta)‘Y’ => 10008 = 1024 (Yotta)‘R’ => 10009 = 1027 (Ronna)‘Q’ => 100010 = 1030 (Quetta)
iec根据国际电工委员会(IEC)标准的自动刻度数字。对于输入数字,接受以下后缀之一。对于输出数字,大于1024的值将被舍入,并使用以下后缀之一打印:‘K’ => 10241 = 210 (Kibi) (uppercase used on output)‘k’ => 10241 = 210 (Kibi) (lowercase accepted on input)‘M’ => 10242 = 220 (Mebi)‘G’ => 10243 = 230 (Gibi)‘T’ => 10244 = 240 (Tebi)‘P’ => 10245 = 250 (Pebi)‘E’ => 10246 = 260 (Exbi)‘Z’ => 10247 = 270 (Zebi)‘Y’ => 10248 = 280 (Yobi)‘R’ => 10249 = 290 (Robi)‘Q’ => 102410 = 2100 (Quebi)iec选项使用单个字母后缀(例如' G '),这不是完全标准的,因为iec标准建议使用两个字母的符号(例如' Gi ') -但在实践中,这种方法很常见。与iec-i选项比较。
iec-i根据国际电工委员会(IEC)标准的自动刻度数字。对于输入数字,接受以下后缀之一。对于输出数字,大于1024的值将被舍入,并使用以下后缀之一打印:‘Ki’ => 10241 = 210 (Kibi) (uppercase used on output)‘ki’ => 10241 = 210 (Kibi) (lowercase accepted on input)‘Mi’ => 10242 = 220 (Mebi)‘Gi’ => 10243 = 230 (Gibi)‘Ti’ => 10244 = 240 (Tebi)‘Pi’ => 10245 = 250 (Pebi)‘Ei’ => 10246 = 260 (Exbi)‘Zi’ => 10247 = 270 (Zebi)‘Yi’ => 10248 = 280 (Yobi)‘Ri’ => 10249 = 290 (Robi)‘Qi’ => 102410 = 2100 (Quebi)iec-i选项使用两个字母的后缀符号(例如' Gi '),正如iec标准所建议的那样,但这在实践中并不常见。与iec选项比较。
autoauto只能和 --from连用。使用这种方法,带有单字母后缀的数字(如“K”)被解释为SI值,带有双字母后缀的数字(如“Ki”)被解释为IEC值。

【busybox】【numfmt】指令介绍

NA

【linux】【numfmt】指令介绍

[root@localhost bin]# numfmt --help
Usage: numfmt [OPTION]... [NUMBER]...
Reformat NUMBER(s), or the numbers from standard input if none are specified.必选参数对长短选项同时适用。--debug          print warnings about invalid input-d, --delimiter=X    use X instead of whitespace for field delimiter--field=FIELDS   replace the numbers in these input fields (default=1)see FIELDS below--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 willright-align; negative N will left-align;padding is ignored if the output is wider than N;the default is to automatically pad if a whitespaceis 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 optionalSUFFIX 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)-z, --zero-terminated    以 NUL 字符而非换行符作为行尾分隔符--help		显示此帮助信息并退出--version		显示版本信息并退出UNIT options:none       no auto-scaling is done; suffixes will trigger an errorauto       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,...FIELDS supports cut(1) style field ranges:N    N'th field, counted from 1N-   from N'th field, to end of lineN-M  from N'th to M'th field (inclusive)-M   from first to M'th field (inclusive)-    all fields
Multiple fields/ranges can be separated with commasFORMAT 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 zero (%010f) width
will zero pad the number. Optional negative values (%-10f) will left align.
Optional precision (%.1f) will override the input determined precision.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.Examples:$ 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 -B1 | numfmt --header --field 2-4 --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 %10fGNU coreutils 在线帮助:<https://www.gnu.org/software/coreutils/>
请向 <http://translationproject.org/team/zh_CN.html> 报告 numfmt 的翻译错误
完整文档请见:<https://www.gnu.org/software/coreutils/numfmt>
或者在本地使用:info '(coreutils) numfmt invocation'

使用示例:

将单个数字 / 转换为人类表示:

$ numfmt --to=si 500000

500k

$ numfmt --to=iec 500000

489K

$ numfmt --to=iec-i 500000

489Ki

$ numfmt --from=si 1M

1000000

$ numfmt --from=iec 1M

1048576

# with '--from=auto', M=Mega, Mi=Mebi

$ numfmt --from=auto 1M

1000000

$ numfmt --from=auto 1Mi

1048576

从' SI '转换到' IEC '刻度

(例如,当驱动器的容量被宣传为' 1TB '时,而检查驱动器的容量给出较低的值):

$ numfmt --from=si --to=iec 1T

932G

指定输入和输出刻度后,支持定义的最大前缀:

$ numfmt --from=si --to=iec-i 2000R

1.6Qi

从输入文件/管道输入转换单个字段

(这些人为的示例仅用于演示目的,因为ls和df都支持——human-readable选项以人类可读的格式输出大小):

#第三个字段(文件大小)将以SI表示方式显示

$ ls -log | numfmt --field 3 --header --to=si | head -n4

-rw-r--r-- 1 94k Aug 23 2011 ABOUT-NLS

-rw-r--r-- 1 3.7k Jan 7 16:15 AUTHORS

-rw-r--r-- 1 36k Jun 1 2011 COPYING

-rw-r--r-- 1 0 Jan 7 15:15 ChangeLog

# Second field (size) will be shown in IEC representation

$ df --block-size=1 | numfmt --field 2 --header --to=iec | head -n4

File system 1B-blocks Used Available Use% Mounted on

rootfs 132G 104741408 26554036 80% /

tmpfs 794M 7580 804960 1% /run/shm

/dev/sdb1 694G 651424756 46074696 94% /home

输出可以使用 --padding或 --format来调整:

# Pad to 10 characters, right-aligned

$ du -s * | numfmt --to=si --padding=10

2.5k config.log

108 config.status

1.7k configure

20 configure.ac

# Pad to 10 characters, left-aligned

$ du -s * | numfmt --to=si --padding=-10

2.5k config.log

108 config.status

1.7k configure

20 configure.ac

# Pad to 10 characters, left-aligned, using 'format'

$ du -s * | numfmt --to=si --format="%10f"

2.5k config.log

108 config.status

1.7k configure

20 configure.ac

# Pad to 10 characters, left-aligned, using 'format'

$ du -s * | numfmt --to=si --padding="%-10f"

2.5k config.log

108 config.status

1.7k configure

20 configure.ac

对于支持分组数字的区域设置,

使用 --grouping或 --format可以进行分组。在' POSIX '区域设置中,分组被静默忽略:

$ LC_ALL=C numfmt --from=iec --grouping 2G

2147483648

$ LC_ALL=en_US.utf8 numfmt --from=iec --grouping 2G

2,147,483,648

$ LC_ALL=ta_IN numfmt --from=iec --grouping 2G

2,14,74,83,648

$ LC_ALL=C numfmt --from=iec --format="==%'15f==" 2G

== 2147483648==

$ LC_ALL=en_US.utf8 numfmt --from=iec --format="==%'15f==" 2G

== 2,147,483,648==

$ LC_ALL=en_US.utf8 numfmt --from=iec --format="==%'-15f==" 2G

==2,147,483,648 ==

$ LC_ALL=ta_IN numfmt --from=iec --format="==%'15f==" 2G

== 2,14,74,83,648==

常用组合指令:

NA

指令不常用/组合用法还需继续挖掘:

NA


http://www.mrgr.cn/news/33801.html

相关文章:

  • 嵌入式系统基础讲解
  • 用apache httpd来实现反向代理
  • golang学习笔记3-变量的声明
  • CORS跨域+Nginx配置、Apache配置
  • 2024.9.22
  • screen使用——关机时在服务器上跑代码
  • 蓝桥杯嵌入式的学习总结
  • UE学习篇ContentExample解读-----------Blueprint_Overview
  • 《深度学习》—— 卷积神经网络(CNN)的简单介绍和工作原理
  • 深度学习与应用:人体关键点检测
  • SpringBoot项目License证书生成与验证(TrueLicense) 【记录】
  • 一种求解无人机三维路径规划的高维多目标优化算法,MATLAB代码
  • Java-Part 0
  • 2009考研数学真题解析-数二:
  • 【JavaWeb】一、Web 开发概述
  • 将相机深度图转接为点云的ROS2功能包
  • Java基础 — 正则表达式+函数式编程
  • 将任意图像增强模型与ultralytics中任意模型进行结合,支持自定义图像增强模块的loss,实现端到端训练
  • Tomcat中间件常见漏洞复现
  • #面试系列-腾讯后端一面