Maison > Tutoriel système > Linux > Le tutoriel de commande numfmt avec des exemples pour les débutants

Le tutoriel de commande numfmt avec des exemples pour les débutants

Christopher Nolan
Libérer: 2025-03-24 09:27:12
original
265 Les gens l'ont consulté

The Numfmt Command Tutorial With Examples For Beginners

The Numfmt command 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 don't bother installing it. In this brief tutorial, let us see the usage of Numfmt command with some practical examples in Linux.

Table of Contents

Numfmt Command Examples

Picture a complex number, for example ‘1003040500’. Some Mathematics experts can easily find the human readable representation of this number in seconds. However, it is bit hard for many people including me. This is where Numfmt command comes in help.

Here are some example commands demonstrating the usage of the numfmt command in Linux.

1. Convert Numbers to Human Readable Format:

Run the following command to convert the given in human readable form.

$ numfmt --to=si 1003040500
1.1G
Copier après la connexion

Let us go for some really long and more 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
Copier après la connexion

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
Copier après la connexion
$ numfmt --to=iec-i 10090008000700060005
8.8Ei
Copier après la connexion

We have seen how to convert the numbers to human readable format. Now let us do the reverse. Meaning - We are going to convert the numbers from human readable format to their actual numeric representations.

2. Convert Numbers from Human Format to Numeric Value:

To convert numbers from the SI (International System of Units) format to its equivalent numeric value, run:

$ numfmt --from=si 1G
1000000000
Copier après la connexion
$ numfmt --from=si 1M
1000000
Copier après la connexion
$ numfmt --from=si 1P
1000000000000000
Copier après la connexion

We can also do this with iec and iec-i standards.

$ numfmt --from=iec 1G
1073741824
Copier après la connexion
$ numfmt --from=iec-i 1Gi
1073741824
Copier après la connexion
$ numfmt --from=auto 1G
1000000000
Copier après la connexion
$ numfmt --from=auto 1Gi
1073741824
Copier après la connexion

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.

3. Formatting a Number with Decimal Precision:

$ echo "12345678.12345678" | numfmt --format "%.2f"
Copier après la connexion

This command formats the number "12345678.12345678" with a decimal precision of two, resulting in the output "12,345,678.12".

4. Converting Binary to Decimal:

$ echo "101010" | numfmt --from=auto --to=decimal
Copier après la connexion

This command converts the binary number "101010" to its decimal equivalent, resulting in the output "42".

5. Formatting Large File Sizes in Human-Readable Format:

$ ls -l | awk '{print $5}' | numfmt --to=iec --suffix=B
Copier après la connexion

This command retrieves the file sizes from the output of the 'ls -l' command, then uses numfmt to format them in a human-readable format using IEC units (e.g., kilobytes, megabytes). The "--suffix=B" option appends the 'B' suffix to indicate bytes.

6. Converting Seconds to Minutes:

$ echo "180" | numfmt --from=auto --to=minutes
Copier après la connexion

This command converts the value "180" representing seconds to minutes, resulting in the output "3".

7. Formatting and Summing Numeric Values from a File:

$ cat numbers.txt | numfmt --field=2 --format="%'.2f" --sum
Copier après la connexion

Assuming the 'numbers.txt' file has multiple lines with two columns, this command formats the values in the second column with a comma as the thousands separator and a decimal precision of two. The "--sum" option calculates and displays the sum of those values.

8. Convert values expressed in SI format to their numeric equivalents:

$ echo 1G | numfmt --from=si
1000000000
Copier après la connexion

This command allows you to convert values expressed in SI format, such as "1G" representing 1 gigabyte, to their numeric equivalents using the "numfmt" utility.

9. Convert values expressed in IEC format to their numeric equivalents:

$ echo 1G | numfmt --from=iec
1073741824
Copier après la connexion

This command allows you to convert values expressed in IEC format, such as "1G" representing 1 gibibyte, to their numeric equivalents using the "numfmt" utility.

10. Display disk space information in human-readable SI units:

$ df -B1 | numfmt --header --field 2-4 --to=si
Copier après la connexion

This command allows you to display disk space information using the df command and convert the sizes to human-readable SI units using the numfmt utility.

11. Display the file sizes in a human-readable format using SI units:

$ ls -l | numfmt --header --field 5 --to=si
Copier après la connexion

This command allows you to list the files and directories with their sizes in a human-readable format using the ls command and the numfmt utility.

Heads Up: 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.

12. Pad to 5 characters, right aligned using '--format' option:

$ du -s * | numfmt --to=si --format="%5f"
Copier après la connexion

13. Pad to 5 characters, left aligned using '--format' option:

$ du -s * | numfmt --to=si --format="%-5f"
Copier après la connexion

14. Pad to 5 characters, right aligned using '--padding' option:

$ du -s * | numfmt --to=si --padding=5
Copier après la connexion

15. Pad to 5 characters, left aligned using '--padding' option:

$ du -s * | numfmt --to=si --padding=-5
Copier après la connexion

For more options and usage, refer man pages.

$ man numfmt
Copier après la connexion

These examples illustrate various scenarios where the numfmt command can be useful for formatting, converting, and processing numeric values in Linux. Feel free to adapt and modify these commands according to your specific needs.

Remember, the specific usage and options for the numfmt command may vary slightly depending on your Linux distribution and the version of coreutils installed.

Frequently Asked Questions

Here's a FAQ (Frequently Asked Questions) for the numfmt command:

1. What is the numfmt command?

The numfmt command is a utility in Linux used for formatting and converting numeric values in various formats.

2. How can I use numfmt to format numbers?

You can use numfmt to format numbers by specifying options such as decimal precision, thousands separators, and padding.

3. Can numfmt convert numbers between different units?

Yes, numfmt can convert numbers between units like bytes, kilobytes, megabytes, etc., making it useful for handling data sizes.

4. Can numfmt handle numbers in scientific notation?

Yes, numfmt can handle numbers in scientific notation and format them according to specified options.

5. Can numfmt be used for batch processing or scripting?

Yes, numfmt can be used in scripts or for batch processing by providing input through command-line arguments or file redirection.

6. Does numfmt support custom formatting options?

Yes, numfmt provides various options to customize formatting, such as specifying the format, padding, and output field width.

7. Which Linux distributions include the numfmt command?

The numfmt command is typically included in coreutils, which is installed by default on most Linux distributions.

8. Can numfmt perform calculations or arithmetic operations on numbers?

No, numfmt is primarily focused on formatting and converting numeric values rather than performing calculations.

9. How can I use numfmt to format numbers in a CSV file?

You can pipe the output of a command that generates CSV data into numfmt to format specific columns or fields.

10. Where can I find more information about numfmt and its usage?

You can refer to the numfmt manual page by typing "man numfmt" in the terminal, or you can access online resources and tutorials for detailed examples and usage instructions.

Conclusion

In conclusion, the numfmt command in Linux is a versatile utility that simplifies the formatting and conversion of numeric values. It provides a convenient way to manipulate numbers, convert units, and customize the representation of data.

Whether you need to format numbers in a specific locale, convert file sizes, or apply custom formatting options, numfmt is a powerful tool at your disposal.

Resource:

  • numfmt manual

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal