AI Generated Cheatsheets

Grep Cheatsheet

Overview

grep is a command-line utility used for searching text files for specific patterns.

Basic Usage

grep pattern file.txt

Searches for pattern in file.txt and prints all lines that match.

Flags

Regular Expressions

grep uses regular expressions to define search patterns. Some common regex syntax includes:

Examples

grep -i "hello" file.txt        # Search for "hello" case-insensitively
grep -v "goodbye" file.txt     # Print all lines that do not contain "goodbye"
grep -rn "error" /var/log      # Recursively search all files in /var/log for "error" and print line numbers
grep -w "the" file.txt         # Match whole words only
grep -E "foo|bar" file.txt     # Match either "foo" or "bar" using extended regex syntax

Resources