2 min read

Searching Text with Grep on macOS Tahoe

grep (Global Regular Expression Print) is a powerful command-line utility used to search for specific patterns of text within files. On macOS Tahoe, it is an essential tool for filtering logs, finding code snippets, and searching through configuration files.

Basic Usage

The basic syntax for grep is:

grep "search_term" filename

Example: To find the word "error" in a file named app.log:

grep "error" app.log

Common Flags

grep becomes much more powerful when you use flags to modify its behavior.

Case Insensitive Search (-i)

By default, grep is case-sensitive. Use -i to ignore case.

grep -i "error" app.log

This will match "Error", "ERROR", and "error".

Recursive Search (-r)

To search through all files in a directory and its subdirectories, use -r.

grep -r "function_name" ~/Projects/my-app/

Show Line Numbers (-n)

To see exactly where the match occurred, use -n.

grep -n "TODO" main.py

Output: 42: # TODO: Fix this bug

Invert Match (-v)

Sometimes you want to see everything except the pattern.

grep -v "DEBUG" app.log

This prints all lines that do not contain "DEBUG".

Using Grep with Pipes

One of the most common ways to use grep on macOS is by piping the output of another command into it.

Filtering Process List

To check if a specific app is running:

ps aux | grep "Chrome"

Filtering History

To find a command you ran previously:

history | grep "git commit"

Regular Expressions

grep supports Regular Expressions (Regex) for advanced pattern matching.

  • ^: Matches the start of a line.
  • $: Matches the end of a line.
  • .: Matches any single character.

Example: Find lines that start with "Error":

grep "^Error" app.log

For more on general terminal usage, refer to Getting Started with macOS Tahoe.