How to use the command numfmt (with examples)

How to use the command numfmt (with examples)

The numfmt command is a tool provided by the Coreutils package that allows you to convert numbers to and from human-readable strings. It provides various options for formatting numbers, such as converting between SI (International System of Units) units and IEC (International Electrotechnical Commission) units, as well as padding and alignment.

Use case 1: Convert 1.5K (SI Units) to 1500

Code:

numfmt --from=si 1.5K

Motivation: In some cases, you may come across numbers represented in SI units (e.g., 1.5K), which may not be desirable for further processing. By converting them to their expanded form (e.g., 1500), you can perform calculations or comparisons more easily.

Explanation:

  • --from=si: This option specifies that the input value is in SI units.

Example output:

1500

Use case 2: Convert the 5th field (1-indexed) to IEC Units without converting the header

Code:

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

Motivation: When working with structured data, it is common to have headers that describe the contents of each column. In this use case, we have a directory listing (ls -l) and want to convert the size column (5th field) to IEC units (e.g., KiB, MiB) without modifying the header line.

Explanation:

  • --header=1: This option specifies that the first line of input is a header and should not be modified.
  • --field=5: This option selects the 5th field (1-indexed) from each input line.
  • --to=iec: This option specifies that the output should be in IEC units.

Example output:

total 12K
drwxr-xr-x  4 user  group  4.0K
-rw-r--r--  1 user  group  2.5K
-rw-r--r--  1 user  group  1.5K

Use case 3: Convert to IEC units, pad with 5 characters, left aligned

Code:

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

Motivation: Sometimes, you may need to format numbers in a specific way, such as padding them with a certain number of characters or aligning them to the left. In this use case, we want to convert the sizes obtained from du -s * to IEC units and pad them with 5 characters, left-aligned.

Explanation:

  • --to=iec: This option specifies that the output should be in IEC units.
  • --format="%-5f": This option specifies the format of the output, with %f representing the number to be formatted. The -5 part pads the number with 5 characters, and the - before 5 indicates left alignment.

Example output:

1.0K
5.5K
12.0K

Conclusion:

The numfmt command is a versatile tool for converting numbers to and from human-readable strings. Whether you need to work with SI or IEC units, format numbers in a specific way, or handle headers in structured data, numfmt provides the necessary options to achieve these transformations.

Related Posts

How to use the command `sqfstar` (with examples)

How to use the command `sqfstar` (with examples)

The command sqfstar is used to create a squashfs filesystem from a tar archive.

Read More
How to use the command 'rc' (with examples)

How to use the command 'rc' (with examples)

The rc command is a modern simplistic port listener and reverse shell, similar to nc (netcat).

Read More
Helm: A Comprehensive Guide to Managing Kubernetes Packages (with examples)

Helm: A Comprehensive Guide to Managing Kubernetes Packages (with examples)

Helm is a package manager for Kubernetes that simplifies the deployment and management of applications on a Kubernetes cluster.

Read More