Computation for Linguists

Learning the Command Line: Regular Expressions & Advanced Grep

Dr. Andrew M. Byrd

2025-09-26

Review

  • What did you learn last time?

Review: wc, grep, and |

  • wc file.txt → identify number of lines, words, and characters in file.txt
    • -l, -w, -c
  • grep "word" file.txt → search for a string in a file
  • Combine with pipes:
    • grep "word" file.txt | wc -l

Review Activity

Using Terminal:

  • Create a folder called alice
  • Download Alice’s Adventures in Wonderland
    • https://www.gutenberg.org/cache/epub/11/pg11.txt
    • name it alice.txt
  • Search for “Alice”
  • How many instances of “Alice” are there in the story? (Or more specifically, how many lines occur in the book that contain at least one “Alice”?)

Regex

What Are Regular Expressions?

  • A language for patterns
    • Pronounced [ˌɹɛg.ˈɛks]
  • Lets us search for:
    • Word forms
    • Phonological environments
    • Structured data

Regular Expressions are WEIRD

  • Today we’ll learn how basic regex commands can be used in the Terminal

  • While we’re currently working in the Terminal, we will return to all of this later in the semester once we have some Python under our belts

Our First Regex

  • Type in the following code:
grep -E "c.t" alice.txt
  • Note the -E
    • This prompts grep to use Extended Regular Expressions
  • What does . do?

Our First Regex

  • . → any single character

Other Useful Flags

  • o → print only matches
  • c → count matches
grep -Eo "c.t" alice.txt
grep -Ec "c.t" alice.txt

Narrowing Down our Search

  • Perhaps we would want to specify certain vowel letters:
grep -E "c[aou]t" alice.txt   

Narrowing Down our Search

  • Perhaps we would want to exclude certain vowel letters:
grep -E "c[^ao]t" alice.txt

Narrowing Down our Search

  • Perhaps we would want to identify a range of certain vowel letters:
grep -E "[a-q]at" alice.txt
  • (Also works for digits: e.g., [0-9])

Narrowing Down our Search

  • What if we want to say “begins with cat”?
grep -E "\scat" alice.txt
  • Note that \s = any whitespace (not just space!)

  • How would we say “ends with cat”?

Narrowing Down our Search

  • How would we say “either cat or cats”?
grep -E "\scats?" alice.txt

Anchors

  • ^ → beginning of line
  • $ → end of line
grep -E "^c.t" alice.txt   # lines starting with "c.t"
grep -E "c.t$" alice.txt    # lines ending with "c.t"

Narrowing Down our Search

  • “(Alice|Rabbit)” → “Alice” or “Rabbit”
grep -E "(Alice|Rabbit)" alice.txt

For a Handy List of Regex

curl -o regex.txt https://winjapati.github.io/301_Fall_2025/3_command_line/14_regex.txt#

Activity: Putting regex to work

Group Activity

  • Within alice.txt, using grep -E, find:
    1. All words beginning with “a”/ “A” or “z” / “Z”;
    2. All four-letter words;
    3. Lines ending with a question mark (use \?);
    4. Words with repeated vowels (“food”, “see”; for this use \1 to capture the exact same text as the previous group; thus ([tp])\1 = “tt”, “pp”);
    5. The total number of articles (“the, The, a, A, an, An”) in the story.