grep (Global Regular Expression Print)
grep is a powerful command-line utility used for searching plain-text data sets for lines that match a regular expression. It is commonly used for searching through files and output from other commands.
Basic Syntax
Commonly Used Options
-i: Ignore case distinctions in patterns and data.-v: Invert the sense of matching, to select non-matching lines.-c: Count the number of matching lines.-l: List only the names of files with matching lines.-L: List only the names of files without matching lines.-n: Prefix each line of output with the line number within its input file.-H: Print the file name for each match.-h: Suppress the file name prefix on output.-ror-R: Read all files under each directory, recursively.-w: Select only those lines containing matches that form whole words.-x: Select only those matches that exactly match the whole line.-A num: Printnumlines of trailing context after matching lines.-B num: Printnumlines of leading context before matching lines.-C num: Printnumlines of output context (both before and after).
Examples
Basic Search
Search for the pattern "foo" in a file:
Ignore Case
Search for the pattern "foo" in a file, ignoring case:
Invert Match
Search for lines that do not contain the pattern "foo":
Count Matches
Count the number of lines that contain the pattern "foo":
List Matching Files
List the names of files that contain the pattern "foo":
List Non-Matching Files
List the names of files that do not contain the pattern "foo":
Show Line Numbers
Show line numbers for lines that contain the pattern "foo":
Recursive Search
Search for the pattern "foo" in all files under the current directory, recursively:
Whole Word Match
Search for the whole word "foo":
Exact Line Match
Search for lines that exactly match "foo":
Context Lines
Print 2 lines of context before and after matching lines:
Print 2 lines of context after matching lines:
Print 2 lines of context before matching lines:
Conclusion
grep is a versatile tool for searching text using patterns. Understanding its options and commands allows for efficient and powerful text searching directly from the command line.