Text-search command reference (CLI)
Course resources · class 2 exercise · class 3 exercise
Use these commands with story.txt, the course copy of 「蜘蛛の糸」. Open a terminal. The last section, Read an agent’s commands, explains the commands in the class 5 recordings.
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 |
Read an agent’s commands
In the class 5 recordings (Claude Code’s and Codex’s response A and recording B), the agents join several commands into one block. Read a block one part at a time, from left to right, and look up each part below. The examples are lines from those recordings; you do not need to run them.
Look at files and help
| Command | Meaning | Example from a recording |
|---|---|---|
cd |
Move to another folder; files are not changed | cd class4-akutagawa |
ls -la |
List every file, including hidden ones, with size and date | ls -la class4-akutagawa |
find . -maxdepth 3 |
List the files in the current folder and its subfolders, down to three levels | find . -maxdepth 3 |
cat |
Print a whole file | cat class4.lx |
head -5, head -c 3000 |
Print only the first 5 lines (10 by default), or the first 3,000 bytes | head -5 works.csv |
sed -n 1,60p |
Print only lines 1–60 | ling matches –help | sed -n 1,60p |
cut -c1-600 |
Keep characters 1–600 of each line | … –tsv –context 0 | head -3 | cut -c1-600 |
wc -l |
Count lines | wc -l works.csv |
--help |
Print how to use a command | ling summarize --help |
which |
Print where a program is installed; nothing if it is missing | which jq |
echo |
Print the text that follows; agents use it to label outputs | echo ---- |
Select and reshape output
| Command | Meaning | Example from a recording |
|---|---|---|
grep -E |
Keep the lines that match a regular expression; inside the quotes, | means “or” |
grep -E 'VERB|落ちる' |
cut -d, -f1-4 |
Keep columns 1–4 of comma-separated lines; without -d,, cut splits at tabs |
cut -d, -f1-4 |
awk '…' |
Run a small program on each line. In the example, for each line whose first tab-separated column is deixis, column 4 is added to the 来る total or the 行く total, depending on column 3; END prints the totals after the last line |
awk -F'' '$1==“deixis”{ … } END{print …}' |
jq '…' |
Select and reshape the JSON lines that ling … --json prints. Inside the quotes, | belongs to jq, not to the shell |
jq -r 'select(.type==“match”) | .data | …' |
python - <<'PY' … PY |
Run the Python program written between the two PY lines |
python - <<'PY' |
Join commands
| Symbol | Meaning | Example from a recording |
|---|---|---|
A | B |
Send the output of A into B (pipe, illustrated) | ling corpus inspect akutagawa.lingcorpus 2>&1 | head -40 |
A; B |
Run A, then B, even if A failed | cd /work; ls -la |
A && B |
Run B only if A succeeded | curl … && unzip -l class4-akutagawa.zip |
2>&1 |
Send error messages to the same place as the output | ling corpus inspect akutagawa.lingcorpus 2>&1 |
2>/dev/null |
Discard error messages | ls ~ 2>/dev/null |
$(…) |
Replace this part with the output of the command inside | file $(which ab-aozora) |
for x in a b; do … done |
Run the commands between do and done once for each value; $x is the current value |
for d in 来る 行く; do … done |
"…$v…" and '…' |
Inside double quotes, $v is replaced by its value; inside single quotes, the text stays as written |
--pattern "$v [lemma=て] [lemma=$d]." |
Create, change, or delete files
These commands write to the disk. Find them in every block, and compare them with what the agent states that it created, changed, or deleted. /tmp is a temporary folder outside the working folder; a file written there is still a created file.
| Command | What it writes | Example from a recording |
|---|---|---|
> FILE |
Saves the output as FILE, replacing a file of the same name | --json > class4-akutagawa/summary.jsonl |
2> FILE |
Saves the error messages as FILE | 2>/tmp/sum.err |
curl -o FILE URL |
Downloads URL as FILE | curl -sSfL -o class4-akutagawa.zip https://… |
unzip |
Extracts the files in a ZIP; -o replaces existing files without asking. unzip -l only lists the contents and writes nothing |
unzip -q class4-akutagawa.zip |
mkdir -p |
Creates a folder | mkdir -p /tmp/aoz_dl |
cat > FILE <<'EOF' … EOF |
Saves the lines between the two EOF lines as FILE |
cat > /tmp/aoz_scripts/list.py <<'EOF' |
rm |
Deletes files without asking | rm /tmp/sum.tsv /tmp/sum.err |
One block, part by part
In Claude Code’s recording B, this block counts matches and works for each combination:
for d in 来る 行く; do for v in '[upos=VERB]' '[lemma=落ちる]'; do echo "== $v + て + $d"; ling summarize --corpus akutagawa.lingcorpus --pattern "$v [lemma=て] [lemma=$d]." --json --by-document | jq -s '{matches: (map(select(.type=="complete"))[0].data.counters.matches_found), works: (map(select(.type=="summary")|.data.document)|unique|length)}' -c; done; done| Part | What it does |
|---|---|
for d in 来る 行く; do … done |
Runs the inner part twice, with $d set to 来る and then 行く |
for v in '[upos=VERB]' '[lemma=落ちる]'; do … done |
Inside that, runs twice more, with $v set to any verb and then 落ちる: four runs in all |
echo "== $v + て + $d" |
Prints a label such as == [lemma=落ちる] + て + 来る |
ling summarize … --pattern "$v [lemma=て] [lemma=$d]." |
Counts one pattern, such as [lemma=落ちる] [lemma=て] [lemma=来る].; --json --by-document prints the counts as JSON lines, one for each work |
| jq -s '{matches: …, works: …}' -c |
Reads all the JSON lines at once (-s) and prints one line (-c): matches is the total number of matches, and works the number of different works that contain one |
The block writes no files. Its output, with a label line before each result:
== [upos=VERB] + て + 来る
{"matches":553,"works":66}
== [lemma=落ちる] + て + 来る
{"matches":10,"works":6}
== [upos=VERB] + て + 行く
{"matches":318,"works":56}
== [lemma=落ちる] + て + 行く
{"matches":1,"works":1}
The unit of matches is matched token sequences, and the unit of works is works with at least one match.