Text layout, tabs and splitting files
Quick answer
Formatting a text display and splitting a file are different jobs. fold wraps long lines, fmt reformats paragraphs, expand and unexpand convert tabs and spaces, and split writes separate output files. Select the command according to whether the original line boundaries must survive.
Prepare the practice environment
Use the chapter fixture described in lab-fixtures/text-streams/README.md. Run setup as a normal user in a disposable Linux environment, enter the printed directory and check pwd. Every example assumes a fresh fixture. Commands omit the terminal prompt; outputs below are not commands.
How it works
fold -w N wraps input at a width; -s prefers breaking at spaces. fmt -w N reflows paragraphs toward a target width and can change line boundaries, so do not use it on structured logs. expand -t N replaces tabs using tab stops every N columns; unexpand -a -t N converts suitable runs of blanks back to tabs, but cannot reconstruct the original byte sequence in general. split -l N -d INPUT PREFIX writes groups of N lines with numeric suffixes (GNU defaults to two digits); the last group can be shorter. split creates actual files, so use a new dedicated directory or a prefix known to be unused. Concatenating the parts in suffix order restores the original line sequence for this fixture.
Wrapping and tab expansion transform the displayed byte stream. Splitting a text file into line-based chunks instead preserves the records in separate files. For the fixed numeric suffixes in this exercise, concatenate the chunks in suffix order and compare the reconstruction with the original using cmp. A visual preview alone may miss differences.
Try the examples
The ten-character fixture is wrapped at width four. No source file changes.
Expected stdout:
abcdefghijOne tab after column one advances to the next four-column stop, inserting three spaces.
Expected stdout:
a bmkdir partssplit -l 2 -d events.txt parts/chunk-ls -1 partscat parts/chunk-00 parts/chunk-01Two lines go into the first file and one into the second. The reconstructed text matches the input. Run in a fresh fixture.
Expected stdout:
chunk-00chunk-01startcheckfinishCommon mistakes
Wrapping text changes line breaks, not merely terminal display. Do not use fmt on records that require exact line boundaries. split can overwrite files with matching names; never reuse an unchecked output prefix.
Practice
In a fresh fixture create a new parts directory, split events.txt into single-line chunks, then verify the order and compare the reconstruction with the original. Expect three parts.
Related references
Study: split, fold, fmt, expand, unexpand. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Text layout and splitting — Knowledge Check
Apply the lesson concepts to the controlled text fixture.
Which tool creates separate output files?
What does expand replace?
Which tool reflows paragraphs?
Why use a fresh split output prefix?