https://www.ostechnix.com/the-numfmt-command-tutorial-with-examples-for-beginners
Today, I cam across an interesting and rather unknown command named “Numfmt”
that converts the numbers to/from human readable format. It reads the
numbers in various representations and reformats them in human readable
format according to the specified options. If no numbers given, it reads
the numbers from the standard input. It is part of the GNU coreutils
package, so you need not bother installing it. In this brief tutorial,
let us see the usage of Numfmt command with some practical examples.
So, if you use si, the numfmt command will auto-scale numbers according to the International System of Units (SI) standard.
The Numfmt also uses the following unit options too.
Numfmt command can also be used in conjunction with other commands. Have a look at the following examples.
You can tweak the output using “–format” or “–padding” options as well.
Pad to 5 characters, right aligned using ‘–format’ option:
Cheers!
Resource:
The Numfmt Command Tutorial With Examples
Picture a complex number, for example ‘1003040500’. Of course the Mathematics ninjas can easily find the human readable representation of this number in seconds. But It is bit hard for me. This is where Numfmt commands comes in help. Run the following command to convert the given in human readable form.$ numfmt --to=si 1003040500 1.1GLet us go for some really long and complex number than the previous number. How about “10090008000700060005”? Bit hard, right? Yes. But the Numfmt command will display the human readable format of this number instantly.
$ numfmt --to=si 10090008000700060005 11EHere, si refers the International System of Units (abbreviated SI from systeme internationale , the French version of the name).
So, if you use si, the numfmt command will auto-scale numbers according to the International System of Units (SI) standard.
The Numfmt also uses the following unit options too.
- iec and iec-i – Auto-scale numbers according to the International Electrotechnical Commission (IEC) standard.
- auto – With this method, numbers with ‘K’,‘M’,‘G’,‘T’,‘P’,‘E’,‘Z’,‘Y’ suffixes are interpreted as SI values, and numbers with ‘Ki’, ‘Mi’,‘Gi’,‘Ti’,‘Pi’,‘Ei’,‘Zi’,‘Yi’ suffixes are interpreted as IEC values.
- none – no auto-scaling.
$ numfmt --to=iec 10090008000700060005 8.8E
$ numfmt --to=iec-i 10090008000700060005 8.8EiWe have seen how to convert the numbers to human readable format. Now let us do the reverse. I.e We are going to convert the numbers from human readable format. To do simply replace “–to” with “–from” option like below.
$ numfmt --from=si 1G 1000000000
$ numfmt --from=si 1M 1000000
$ numfmt --from=si 1P 1000000000000000We can also do this with iec and iec-i standards.
$ numfmt --from=iec 1G 1073741824
$ numfmt --from=iec-i 1Gi 1073741824
$ numfmt --from=auto 1G 1000000000
$ numfmt --from=auto 1Gi 1073741824Like I already mentioned, when using “auto”, the numbers with ‘K’,‘M’,‘G’,‘T’,‘P’,‘E’,‘Z’,‘Y’ suffixes are interpreted as SI values, and numbers with ‘Ki’, ‘Mi’,‘Gi’,‘Ti’,‘Pi’,‘Ei’,‘Zi’,‘Yi’ suffixes are interpreted as IEC values.
Numfmt command can also be used in conjunction with other commands. Have a look at the following examples.
$ echo 1G | numfmt --from=si 1000000000
$ echo 1G | numfmt --from=iec 1073741824
$ df -B1 | numfmt --header --field 2-4 --to=si
$ ls -l | numfmt --header --field 5 --to=siPlease note that the ls and df commands already have “–human-readable” option to display the outputs in human readable form. The above examples are given just for the demonstration purpose only.
Pad to 5 characters, right aligned using ‘–format’ option:
$ du -s * | numfmt --to=si --format="%5f"Pad to 5 characters, left aligned using ‘–format’ option:
$ du -s * | numfmt --to=si --format="%-5f"Pad to 5 characters, right aligned using ‘–padding’ option:
$ du -s * | numfmt --to=si --padding=5Pad to 5 characters, left aligned using ‘–padding’ option:
$ du -s * | numfmt --to=si --padding=-5For more options and usage, refer man pages.
$ man numfmtAnd, that’s all for now. More good stuffs to come. Stay tuned!
Cheers!
Resource:
No comments:
Post a Comment