← All Exercises

Render an Aligned Table with printf

basicbash~20min

Background

CLI reports look broken when columns drift. printf field widths give stable alignment without external table libraries or extra dependencies.

Problem

Given records in name:size:owner form, print a fixed-width table with a header, right-aligned sizes, and names truncated to at most 12 characters.

Input

A readable file with name:size:owner records; blank lines and lines beginning with # are ignored; size must be an unsigned integer.

Output

A header line, then one aligned row per record with the name left-aligned, the size right-aligned, and the owner last; invalid sizes exit non-zero.

Sample tests

Sample 1

Input: readme.md:2048:alice
# ignored
application-2026.log:1048576:bob
Expected: NAME               SIZE  OWNER
readme.md          2048  alice
applicati...    1048576  bob

Sample 2

Input: an empty file
Expected: NAME               SIZE  OWNER

Your solution

Learning objective

Use printf width specifiers, IFS-driven reads, and guarded truncation for dependency-free table output.