Text format surprises
On this page
Two files contain the same words, yet one is larger. Another pair looks aligned on screen, but an editor moves across their gaps differently. Reading the visible letters does not explain either difference.
By the end, you can inspect line endings, distinguish UTF-8 bytes from characters, and reveal tabs or a missing final newline without changing the file.
Before you start
Section titled “Before you start”Finish Editing with Vim (B07.4), or defer only its editor exercise if your installation lacks the tested vi build. You need B06.3’s file command and the earlier wc examples, not a running editor.
Keep your B04 practice directory. Use only ~/lab/b07/formats. Run this setup once; use Reset this lesson before repeating it.
mkdir -p ~/lab/b07/formatscd ~/lab/b07/formatsprintf 'alpha\nbeta\n' > lf.txtprintf 'alpha\r\nbeta\r\n' > crlf.txtprintf 'café\n' > utf8.txtprintf 'name\tvalue\n' > tabs.txtprintf 'name value\n' > spaces.txtprintf 'last line' > no-final.txtThese are disposable text fixtures. printf interprets \n as a newline, \r as carriage return, and \t as a tab. Copy the lines as written; B08 and B09 explain the shell syntax. The spaces in the spaces.txt setup are four ordinary spaces.
Reveal line separators
Section titled “Reveal line separators”Ask file to identify the two endings:
file lf.txt crlf.txtlf.txt: ASCII textcrlf.txt: ASCII text, with CRLF line terminatorsLF, line feed, is the newline byte used by these Unix-style text files. CRLF stores carriage return followed by line feed, a convention used by many Windows text files. An editor may show both as an ordinary line break. The identification here is evidence about these files, not a rule that every file from one operating system has one format.
Use cat -A to expose nonprinting characters and line ends:
cat -A lf.txtalpha$beta$cat -A crlf.txtalpha^M$beta^M$Here $ marks a newline; it is display notation added by cat, not a dollar sign stored in the file. ^M marks the carriage return. Read ^M$ as a CR followed by a newline, not a command or an editor shortcut.
learner@rootvolt:~/lab/b07/formats$ cat -A lf.txtalpha$
beta$learner@rootvolt:~/lab/b07/formats$ cat -A crlf.txtalpha^M$
beta^M$learner@rootvolt:~/lab/b07/formats$ Steps (6)
- Type
cat -A lf.txt - Press Enter
- Output:
alpha$ beta$ - Type
cat -A crlf.txt - Press Enter
- Output:
alpha^M$ beta^M$
The static transcript makes the same comparison: the words match, while each CRLF line has an additional carriage return marker.
The text view in this model treats each separator as a line break. The bracketed [CR] and [LF] labels expose the stored controls beside that view; they are not a captured terminal screen or literal file characters. Other renderer behavior is omitted.
Predict first: with two lines, how many extra bytes does the CRLF fixture store?
wc -c lf.txtwc -c crlf.txtRun it, then compare with your guess
11 lf.txt13 crlf.txtTwo extra bytes: one carriage return for each of the two lines. The words did not change.
For another view, od displays stored data rather than passing control bytes straight to the terminal. Its -c option uses printable characters and escape or numeric representations:
od -c crlf.txt0000000 a l p h a \r \n b e t a \r \n0000015Look for \r \n after each word. The left column and final number are offsets in octal, base eight, by default. They are not line numbers; use wc -c when you want a decimal byte count without converting those offsets.
Count characters separately from bytes
Section titled “Count characters separately from bytes”cat utf8.txtcaféfile utf8.txtutf8.txt: Unicode text, UTF-8 textUTF-8 is an encoding that represents Unicode characters as stored bytes. A character need not occupy one byte. In this fixture, é uses two; the other three letters each use one, and the newline adds one.
wc -c utf8.txt6 utf8.txtwc -m utf8.txt5 utf8.txt-c counts bytes. -m counts characters under the active locale, here en_US.UTF-8. The five characters include the newline. A character count is not a count of displayed columns.
learner@rootvolt:~/lab/b07/formats$ cat utf8.txtcafélearner@rootvolt:~/lab/b07/formats$ file utf8.txtutf8.txt: Unicode text, UTF-8 textlearner@rootvolt:~/lab/b07/formats$ wc -c utf8.txt6 utf8.txtlearner@rootvolt:~/lab/b07/formats$ wc -m utf8.txt5 utf8.txtlearner@rootvolt:~/lab/b07/formats$ Steps (12)
- Type
cat utf8.txt - Press Enter
- Output:
café - Type
file utf8.txt - Press Enter
- Output:
utf8.txt: Unicode text, UTF-8 text - Type
wc -c utf8.txt - Press Enter
- Output:
6 utf8.txt - Type
wc -m utf8.txt - Press Enter
- Output:
5 utf8.txt
The file identification, byte count, and character count remain readable in the static transcript. No conversion or repair happened: every command only inspected the file.
od -c utf8.txt0000000 c a f 303 251 \n0000006The two octal byte values 303 251 belong to é. This byte-oriented representation is not evidence that the correctly rendered word is damaged. Interpret a diagnostic display using the tool’s format and the file’s encoding.
Tell a tab from several spaces
Section titled “Tell a tab from several spaces”A tab is one stored control character. Its display advances to a tab stop; the visible gap depends on the current column and the viewer. Spaces are individual characters. They can produce a similar-looking gap without storing the same data.
cat -A tabs.txtname^Ivalue$cat -A spaces.txtname value$^I marks the tab. The second output contains four ordinary spaces instead. Neither ^I nor the end marker $ was added to the file.
learner@rootvolt:~/lab/b07/formats$ cat -A tabs.txtname^Ivalue$learner@rootvolt:~/lab/b07/formats$ cat -A spaces.txtname value$learner@rootvolt:~/lab/b07/formats$ Steps (6)
- Type
cat -A tabs.txt - Press Enter
- Output:
name^Ivalue$ - Type
cat -A spaces.txt - Press Enter
- Output:
name value$
The transcript shows the full distinction without playback. When alignment matters to a format or editor, inspect the characters rather than judging only the apparent gap.
Notice a missing final newline
Section titled “Notice a missing final newline”The last fixture contains visible text but no newline at its end:
cat -A no-final.txtlast linewc -l no-final.txt0 no-final.txtThere is no $ end marker in that cat -A output. wc -l counts newline characters, so its count is zero even though a reader sees words. This is why “line count” can differ from the number of visible text fragments. It does not mean the file has zero bytes.
Mistakes to recognize
Section titled “Mistakes to recognize”Treating diagnostic markers as contents. Do not replace the file’s ending by typing the literal characters ^M$. They are one tool’s representation. od -c uses \r\n for the same pair of controls.
Using a byte count as a character count. B06 introduced byte sizes. UTF-8 explains why those sizes can exceed the number of visible letters; use the option that answers your question.
Changing text before identifying the difference. These inspections preserve the evidence. Line endings, encoding, and spacing are separate properties; a different display is not by itself a reason to rewrite a file.
Try it yourself
Section titled “Try it yourself”Goal: Explain a handoff containing more than one format detail, using evidence rather than appearance.
printf 'café\tready\r\n' > handoff.txtIdentify its encoding, separator, and whitespace character. Then predict and check its byte, character, and newline counts. Keep the file unchanged.
Hint
Use file for identification and od -c for stored controls. Count the tab, carriage return, and newline as characters too; é occupies two bytes in this fixture.
Solution
file handoff.txthandoff.txt: Unicode text, UTF-8 text, with CRLF line terminatorsod -c handoff.txt0000000 c a f 303 251 \t r e a d y \r \n0000015wc -c handoff.txt13 handoff.txtwc -m handoff.txt12 handoff.txtwc -l handoff.txt1 handoff.txtIt contains UTF-8 text, a tab, and CRLF. The counts are 13 bytes, 12 characters, and one newline. The accented character accounts for the one-byte difference between the first two counts.
Reset this lesson
Section titled “Reset this lesson”Inspect only this lesson’s directory from its parent:
cd ~/lab/b07ls -AR formatsIt contains the six initial fixtures and, after the challenge, handoff.txt.
rm -r ~/lab/b07/formatsRerun Before you start. If the directory is already absent, skip removal. This restores the original byte sequences, not a converted version of the files.
- LF and CRLF use different stored line separators; CRLF includes an extra carriage return per line.
cat -Aandod -cexpose controls using display notation.- UTF-8 characters can occupy multiple bytes;
wc -candwc -manswer different questions. - Tabs and spaces can look similar while containing different characters.
wc -lcounts newlines, including the effect of a missing final newline.
Next, Practice: Reading and Editing Text combines the five lessons in one handoff.
Further reading
Section titled “Further reading”- On your system:
man cat,man od,man wc, andman file. - GNU Coreutils: cat invocation, od invocation, and wc invocation.
- Unicode UTF-8 FAQ, for the relationship between characters, encoding, and stored bytes.