Skip to content

Practice: Reading and Editing Text

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM)
On this page

A teammate gives you a log and two unfinished handoff files. Before returning them, you need to inspect the log, watch a completion message arrive, and save the note and configuration with the agreed status. Two supporting text files also need a format explanation.

This practice combines all five B07 lessons. Documentation and earlier lessons are allowed; the evidence matters more than memorizing keys.

Finish Viewing short and long files through Text format surprises (B07.1–B07.5). The editor task uses the course VM’s vi, which runs Vim-tiny, as established in B07.4. If that tested editor is absent from your system, defer its goal until B12 rather than substituting an unverified program.

Keep your existing B04 practice directory. Use only disposable ~/lab/b07/practice; all earlier B07 directories remain outside it. Run this setup once, or use Reset this lesson before repeating it.

Terminal window
mkdir -p ~/lab/b07/practice
cd ~/lab/b07/practice
printf 'Line %02d: routine check\n' {1..39} > handoff.log
printf 'Line 40: CHECKPOINT reached\n' >> handoff.log
printf 'Line %02d: routine check\n' {41..80} >> handoff.log
printf 'Inspection pending\n' > handoff.txt
printf 'status=draft\n' > handoff.conf
printf 'Handoff begins\n' > events.log
printf 'name\tvalue\n' > columns.txt
printf 'café\r\n' > received.txt

These lines build the handoff on your VM; you do not need to download a fixture. The log has numbered entries and a checkpoint. The note and configuration begin unfinished. The final two files contain spacing and encoding details for inspection. Copy the setup exactly, including the accented character and escape sequences introduced in B07.5.

Reach these five goals, in order:

  1. You have read the first and last two entries of handoff.log, found its checkpoint in a pager with line numbers, and returned to Bash. You can state the log’s newline count and the checkpoint’s line number without printing the whole log.
  2. You have watched events.log from a second terminal while the first appends handoff: reviewed. You have stopped watching deliberately and verified that both saved lines remain. Both terminals use ~/lab/b07/practice.
  3. Nano has saved handoff.txt with the single line Inspection complete. It ends with one newline and no extra blank line. You have verified the saved result after leaving Nano.
  4. Vim-tiny, launched as vi handoff.conf, has changed only the draft value to ready. You used a search to locate it, saved, exited, and read the result from Bash.
  5. You can identify the encoding and line ending of received.txt, explain its different byte and character counts, and identify whether columns.txt contains a tab or ordinary spaces between its words. Preserve both files.

Your terminal and explanations establish:

  • The two boundary pairs, a pager search result, and the log count.
  • A completion line arriving while the watcher is running, then a returned prompt after interruption and a read of the saved file.
  • A saved one-line Nano note, with exactly one end marker when inspected using B07.5’s diagnostic view.
  • A saved configuration with status=ready, produced through vi rather than by rewriting it with a shell command.
  • A file identification, a stored-byte view, separate byte/character counts, and visible whitespace evidence for the supporting files.

The editor results in the solution were captured on the course VM. The noninteractive inspection and reset evidence comes from the Ubuntu lab. No full-screen program is simulated as a replay.

Hint for goal 1

Use head and tail for boundaries. In less -N, the forward search begins with a slash and q returns to Bash. wc -l counts newline characters.

Hint for goal 2

Run tail -f in the window you will leave watching. In the other, use B07.2’s exact echo 'message' >> file pattern. Ctrl+C belongs in the watcher window.

Hint for goal 3

Nano’s Ctrl+K cuts the current line. Starting from the single original line, one cut is enough. Type its replacement without adding an extra Enter; confirm the saved ending with cat -A.

Hint for goal 4

From Normal mode, a search positions the cursor on draft. cw changes the word and enters Insert. Esc returns before the save command.

Hint for goal 5

file identifies the text, od -c exposes stored controls, and wc -c differs from wc -m. cat -A distinguishes a tab from a gap made of spaces.

Solution

Goal 1: inspect without flooding the terminal

Terminal window
cd ~/lab/b07/practice
Terminal window
head -n 2 handoff.log
Captured output
Line 01: routine check
Line 02: routine check
Terminal window
tail -n 2 handoff.log
Captured output
Line 79: routine check
Line 80: routine check
Terminal window
wc -l handoff.log
Captured output
80 handoff.log
Terminal window
less -N handoff.log

Type /CHECKPOINT, Enter. The checkpoint is on line 40, as in the fixture inspected in B07.1. Press q to return. The file has 80 newlines; reading did not change them.

Goal 2: observe, interrupt, then inspect

In the right terminal:

Terminal window
cd ~/lab/b07/practice
tail -f events.log

In the left terminal:

Terminal window
cd ~/lab/b07/practice
echo 'handoff: reviewed' >> events.log

Copy the append line as it is; >> adds a line to the end of the file, and B09 explains it. The right terminal receives the appended line. It stays occupied by the watcher until you press Ctrl+C there.

After stopping it, run pwd to check that Bash accepts a command again, then read the file:

Terminal window
pwd
Captured output
/home/learner/lab/b07/practice
Terminal window
cat events.log
Captured output
Handoff begins
handoff: reviewed

The two-window method is the one shown in B07.2’s screenshots. The saved-file output above is the lab’s actual read after the same append command, not a recording of two concurrent terminals.

Goal 3: save the single-line Nano note

Terminal window
nano handoff.txt

Starting from the original Inspection pending line:

  1. Press Ctrl+K once to cut that line.
  2. Type Inspection complete without pressing Enter.
  3. Press Ctrl+O, Enter, then Ctrl+X.
Terminal window
cat -A handoff.txt
Captured output
Inspection complete$

This output was captured on the course VM. One $ marks the final newline. A second line containing only $ would reveal an unwanted blank line. The shown marker is diagnostic notation, not part of the note.

Goal 4: change the configuration in vi

Terminal window
vi handoff.conf
  1. Press Esc, then type 1G.
  2. Type /draft, Enter.
  3. Type cw, then ready, then Esc.
  4. Type :w, Enter, then :q, Enter.
Terminal window
cat handoff.conf
Captured output
status=ready

This saved result was captured on the course VM. If you lose track of the mode, Esc returns from Insert before an editor command. Do not type a shell command into the file to inspect it.

Goal 5: describe stored text rather than appearance

Terminal window
file received.txt
Captured output
received.txt: Unicode text, UTF-8 text, with CRLF line terminators
Terminal window
od -c received.txt
Captured output
0000000 c a f 303 251 \r \n
0000007
Terminal window
wc -c received.txt
Captured output
7 received.txt
Terminal window
wc -m received.txt
Captured output
6 received.txt
Terminal window
cat -A columns.txt
Captured output
name^Ivalue$

received.txt contains UTF-8 text and CRLF. The accented character uses two bytes; carriage return and newline each count as characters too. It therefore has seven bytes and six characters. columns.txt contains a tab, represented by ^I, rather than four stored spaces.

Answer without running anything, then open each answer.

1. Which tool would you choose to search a long file while keeping it open? Which two tools would you use to read only its beginning and end?

Answer

Use less for paging and searching; -N adds line numbers. Use head for the beginning and tail for the end, with -n to choose the count.

2. A watcher prints nothing for a while and has no Bash prompt. Does that prove the shell is stuck? Where do you type an append command?

Answer

No. tail -f may be waiting for additions. Type the append command in the other terminal, which has a prompt and addresses the same file. Ctrl+C in the watcher stops it and returns control to Bash.

3. In Nano, does an asterisk mean the file is already saved? What do Ctrl+O and Ctrl+X do?

Answer

The asterisk marks unsaved buffer changes. Ctrl+O writes the buffer to the chosen filename; Enter accepts that filename. Ctrl+X exits, asking about remaining unsaved changes when needed.

4. In this VM’s vi, what changes the meaning of an ordinary letter key? How do you save after typing a replacement word?

Answer

The mode changes its meaning. Normal interprets editing keys, while Insert adds file text. Press Esc to return from Insert, then type :w, Enter. Use :q, Enter, to exit afterward; E37 means unsaved changes remain.

5. Does a wider-looking gap prove that a file contains more spaces? What do ^I and ^M$ mean in these cat -A inspections?

Answer

No. A tab advances to a tab stop and can resemble several spaces. ^I represents a tab. ^M$ represents a carriage return followed by a newline. These are display markers, not literal text added to the file.

6. Can a UTF-8 byte count exceed its character count? Can a file with visible text have a zero result from wc -l?

Answer

Yes to both. Some UTF-8 characters occupy multiple bytes. wc -l counts newline characters, so visible text without a final newline can have a zero newline count.

Stop any watcher with Ctrl+C and close the practice editors deliberately. Inspect only this practice directory from its parent:

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

It contains columns.txt, events.log, handoff.conf, handoff.log, handoff.txt, and received.txt. Editing their contents did not change those names.

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

Rerun Before you start to restore the pending note, draft configuration, original log, and supporting files. If the directory is absent, skip removal. Both editor goals must then be performed again to reach the completed handoff.

  • Choose a reader for the task: complete short text, selected boundaries, or a searchable long file.
  • Follow additions in one terminal, append from another, and interrupt deliberately.
  • Save and exit each editor using its own controls, then verify from Bash.
  • Inspect line endings, encoding, and whitespace before explaining a text difference.

Next, Bash and other shells begins B08 by distinguishing shell behavior, builtins, aliases, and external programs.