Wednesday, May 9, 2018

The Numfmt Command Tutorial With Examples For Beginners

https://www.ostechnix.com/the-numfmt-command-tutorial-with-examples-for-beginners


numfmt command
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.

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.1G
Let 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
11E
Here, 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.
Here is some more examples for the above options.
$ numfmt --to=iec 10090008000700060005
8.8E
$ numfmt --to=iec-i 10090008000700060005
8.8Ei
We 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
1000000000000000
We 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
1073741824
Like 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=si
Please 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.
You can tweak the output using “–format” or “–padding” options as well.
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=5
Pad to 5 characters, left aligned using ‘–padding’ option:
$ du -s * | numfmt --to=si --padding=-5
For more options and usage, refer man pages.
$ man numfmt
And, that’s all for now. More good stuffs to come. Stay tuned!
Cheers!
Resource:

No comments:

Post a Comment