Text-search command reference
Course resources · class 2 exercise · class 3 exercise
Use these commands with story.txt, the course copy of 「蜘蛛の糸」. Open a terminal.
Folders and input
| Command | Meaning |
|---|---|
pwd |
Show the current folder’s path |
ls |
List its contents |
mkdir -p dh-b-2026 |
Create a working folder if needed |
cd dh-b-2026 |
Move into that folder from its parent; run once |
The download overwrites any existing story.txt in the current folder. Keep annotations in a separate file.
curl -fL -o story.txt https://soranoha.org/works/000092_000879/plaintext-f fails on HTTP errors; -L follows redirects; -o sets the output file path.
Search and counting units
| Command | Output |
|---|---|
grep --color=auto -nF 'のぼ' story.txt |
Matching lines with line numbers |
grep -cF 'のぼ' story.txt |
Number of matching lines |
grep -oF 'のぼ' story.txt |
Each occurrence on a separate line |
--color=auto highlights matches in a terminal on macOS and Linux without altering matches or counts.
To count occurrences, pipe the output:
grep -oF 'のぼ' story.txt | wc -l-F matches exact strings. A pipe (|) sends output into the next command; wc -l prints the incoming line count. Use straight quotes ('), not curved quotes (‘ ’). Output after each stage, illustrated.
The bundled text has 5 matching lines and 12 occurrences, including あのぼんやり. Counting units illustrated.
How can I save search output to a file?
Paste the command and output into your notes, or use > to save the output:
grep -nF '落ち' story.txt > down.txt> writes the output to down.txt, replacing a file of the same name. Keep the command in your notes as well.
Search with a pattern
With -E, grep reads its argument as a regular expression. It cannot be combined with -F.
| Command | Output |
|---|---|
grep -onE 'のぼ.' story.txt |
のぼ and the next character, one per line with line numbers |
grep -oE 'のぼ.' story.txt | sort | uniq -c |
A count for each following character |
grep -oE 'のぼ[っりるれ]' story.txt | wc -l |
Occurrences followed by one of the bracketed characters |
| Symbol | Meaning |
|---|---|
. |
Any one character |
[っりるれ] |
One character from inside the brackets |
The bundled text has 12 matches for のぼ. (including one のぼん) and 11 for のぼ[っりるれ].
Recover from a problem
| Problem | Action |
|---|---|
| Download fails | Save the local text copy as story.txt in the working folder |
No such file |
Check the current folder and filename with pwd and ls |
| Terminal waits for more input | Press Ctrl+C; check that both quotes are present |
| No matches | Check the spelling in the source |
. does not match one character and the output is garbled |
Check that locale shows UTF-8, and set the terminal’s character encoding to UTF-8 |
grep is not recognized on Windows |
Close Command Prompt or PowerShell; open Ubuntu on WSL |
| A command is unavailable | Follow Set up ling; for the class 2 homework, browser search also works |