Skip to content

Text format surprises

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
Prerequisites
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.

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.

Terminal window
mkdir -p ~/lab/b07/formats
cd ~/lab/b07/formats
printf 'alpha\nbeta\n' > lf.txt
printf 'alpha\r\nbeta\r\n' > crlf.txt
printf 'café\n' > utf8.txt
printf 'name\tvalue\n' > tabs.txt
printf 'name value\n' > spaces.txt
printf 'last line' > no-final.txt

These 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.

Ask file to identify the two endings:

Terminal window
file lf.txt crlf.txt
Captured output
lf.txt: ASCII text
crlf.txt: ASCII text, with CRLF line terminators

LF, 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:

Terminal window
cat -A lf.txt
Captured output
alpha$
beta$
Terminal window
cat -A crlf.txt
Captured output
alpha^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.

Expose LF and CRLF endings
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. cat -A shows $ for LF endings and ^M$ for CRLF endings in these fixtures.
Steps (6)
  1. Type cat -A lf.txt
  2. Press Enter
  3. Output: alpha$ beta$
  4. Type cat -A crlf.txt
  5. Press Enter
  6. 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.

Visible text and stored line separatorsBoth files contain the visible words alpha and beta on separate lines. The LF file stores one LF after each word. The CRLF file stores CR then LF after each word. Bracketed control names are diagram markers, not literal file characters. LF fileText viewalphabetaStored endingsalpha [LF]beta [LF] CRLF fileText viewalphabetaStored endingsalpha [CR][LF]beta [CR][LF] CR and LF mark stored control bytes, not visible letters.
Visible text and stored line separatorsBoth files contain the visible words alpha and beta on separate lines. The LF file stores one LF after each word. The CRLF file stores CR then LF after each word. Bracketed control names are diagram markers, not literal file characters. LF fileText viewalphabetaStored endingsalpha [LF]beta [LF] CRLF fileText viewalphabetaStored endingsalpha [CR][LF]beta [CR][LF] CR and LF mark control bytes.
Simplified model. Both files contain alpha and beta; the stored separators are LF in one and CR followed by LF in the other.

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?

Terminal window
wc -c lf.txt
wc -c crlf.txt
Run it, then compare with your guess
LF byte count
11 lf.txt
CRLF byte count
13 crlf.txt

Two 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:

Terminal window
od -c crlf.txt
Captured output
0000000 a l p h a \r \n b e t a \r \n
0000015

Look 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.

Terminal window
cat utf8.txt
Captured output
café
Terminal window
file utf8.txt
Captured output
utf8.txt: Unicode text, UTF-8 text

UTF-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.

Terminal window
wc -c utf8.txt
Captured output
6 utf8.txt
Terminal window
wc -m utf8.txt
Captured output
5 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.

Count bytes and characters in UTF-8
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The café line contains six bytes but five characters, including its newline.
Steps (12)
  1. Type cat utf8.txt
  2. Press Enter
  3. Output: café
  4. Type file utf8.txt
  5. Press Enter
  6. Output: utf8.txt: Unicode text, UTF-8 text
  7. Type wc -c utf8.txt
  8. Press Enter
  9. Output: 6 utf8.txt
  10. Type wc -m utf8.txt
  11. Press Enter
  12. 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.

Terminal window
od -c utf8.txt
Captured output
0000000 c a f 303 251 \n
0000006

The 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.

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.

Terminal window
cat -A tabs.txt
Captured output
name^Ivalue$
Terminal window
cat -A spaces.txt
Captured output
name value$

^I marks the tab. The second output contains four ordinary spaces instead. Neither ^I nor the end marker $ was added to the file.

Reveal tabs and spaces
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. cat -A reveals a tab as ^I while ordinary spaces remain spaces.
Steps (6)
  1. Type cat -A tabs.txt
  2. Press Enter
  3. Output: name^Ivalue$
  4. Type cat -A spaces.txt
  5. Press Enter
  6. 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.

The last fixture contains visible text but no newline at its end:

Terminal window
cat -A no-final.txt
Captured output
last line
Terminal window
wc -l no-final.txt
Captured output
0 no-final.txt

There 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.

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.

Goal: Explain a handoff containing more than one format detail, using evidence rather than appearance.

Terminal window
printf 'café\tready\r\n' > handoff.txt

Identify 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
Terminal window
file handoff.txt
Captured output
handoff.txt: Unicode text, UTF-8 text, with CRLF line terminators
Terminal window
od -c handoff.txt
Captured output
0000000 c a f 303 251 \t r e a d y \r \n
0000015
Terminal window
wc -c handoff.txt
Captured output
13 handoff.txt
Terminal window
wc -m handoff.txt
Captured output
12 handoff.txt
Terminal window
wc -l handoff.txt
Captured output
1 handoff.txt

It 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.

Inspect only this lesson’s directory from its parent:

Terminal window
cd ~/lab/b07
ls -AR formats

It contains the six initial fixtures and, after the challenge, handoff.txt.

Terminal window
rm -r ~/lab/b07/formats

Rerun 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 -A and od -c expose controls using display notation.
  • UTF-8 characters can occupy multiple bytes; wc -c and wc -m answer different questions.
  • Tabs and spaces can look similar while containing different characters.
  • wc -l counts newlines, including the effect of a missing final newline.

Next, Practice: Reading and Editing Text combines the five lessons in one handoff.