grep Basics
grep is a powerful command-line utility used for searching text patterns in files or standard input. Below, we cover the basics of grep, including its syntax, options, and practical examples.
Basic Syntax
The basic syntax for grep is:
grep [options] pattern [file...]pattern: The text or regular expression to search for.file: The file(s) to search.
If no file is specified, grep reads from standard input.
Commonly Used Options
-i: Ignore case distinctions in the pattern.-v: Invert the match to select non-matching lines.-ror-R: Recursively search files in a directory.-l: Print only the names of files with matches.-c: Count the number of matching lines.-n: Prefix each line of output with its line number.-H: Print the filename for each match (useful when searching multiple files).-E: Interpret the pattern as an extended regular expression (ERE).--color: Highlight matching strings in the output.
Examples
Basic Search
Search for lines containing the word "error":
Ignore Case
Search for "error" regardless of case:
Invert Match
Display lines that do not contain "error":
Recursive Search
Search for "TODO" in all files in the current directory and subdirectories:
Show Line Numbers
Print matching lines with their line numbers:
Search Multiple Files
Search for "error" in multiple files:
Count Matches
Count the number of lines containing "error":
Show Only Filenames
List filenames containing matches:
Use Regular Expressions
Search for lines that start with "ERROR":
Search for lines ending with ".log":
Highlight Matches
Highlight the matching text:
Combine Options
Search recursively, ignore case, and show line numbers:
Advanced Examples
Search for Whole Words
Match the whole word "error" (not substrings like "errors"):
Search with Extended Regular Expressions
Use extended regex to search for "error" or "warning":
Search Specific File Types
Search for "TODO" only in .c files:
Exclude Specific Files
Search for "TODO" but exclude .log files:
Summary
grep is an essential tool for text processing, offering versatile options for searching patterns efficiently. By combining its features with regular expressions and options, you can perform complex searches and analyze data effectively.
Last updated