grep searches text file for rows that match a specified regular expression
Command format
grep [OPTIONS] PATTERN [FILE ...]
Command parameters
Generic Program Information
--help
Printing Help information
-V,--version
Print version Information
Matcher Selection
-E,--extended-regexp
Use the basic regular expression (ERE) to interpret the PATTERN
-F,--fixed-strings
Each pattern is treated as a set of fixed strings (separated by new lines) and not as regular expressions.
-G,--basic-regexp
Use the basic regular expression (BRE) to interpret the PATTERN
-P,--perl-regexp
Use Perl Regular Expressions (PCRE) to interpret the PATTERN
Matching Control
-E PATTERN,--regexp=pattern
Using pattern as a matching pattern
-F FILE,--file=file
Get a matching pattern from a file
-I.,--ignore-case
Ignore case
-V,--invert-match
Output rows with no matches
-W,--word-regexp
Exact word matching
-X,--line-regexp
Line Exact Match
General Output Control
-C,--count
The number of output matches
-L,--files-with-matches
Output contains the file name of the match instead of the direct output matching line
-L,--files-without-match
Similar to the-l option, the output is a file name that contains a mismatch
-M NUM,--max-count=num
Stop reading files after matching NUM
-O,--only-matching
Show only matches instead of matching rows
-Q,--quiet,--silent
Quiet mode, no information displayed
-S,--no-messages
Do not display an error message when the file does not exist or is unreadable
Output Line Prefix Control
-B,--byte-offset
Precede each matching line with the block number of the line within the file
-H,--with-filename
Add a file name before each matching line
-H,--no-filename
Suppress file name output when multiple file searches
-N,--line-number
Precede each matching line with the line number of the line within the file
Context Line Control
-A NUM,--after-context=num
Outputs the contents of a matching row and its subsequent NUM rows
-B NUM,--before-context=num
Outputs the contents of a matching row and its previous NUM rows
-C NUM,-num,--context=num
Outputs the contents of a matching row and its front and rear NUM rows
Instance
Test file Test.txt:
The Zen of Python, by Tim petersbeautiful are better than uglyexplicit is better than implicitsimple are better than complex Complex is better than complicatedflat be better than Nestedsparse is better than densereadability countsspecial cases N ' t special enough to break the Rulesalthough practicality beats Purityerrors should never pass silentlyunless explicitly Silencedin the face of ambiguity, refuse the temptation to Guessthere should is one--and preferably only one--obvious WA Y to does italthough that the obvious at first unless you ' re dutchnow are better than Neveralthough never is often Better than *right* nowif the implementation are hard-to-explain, it's a bad ideaif the implementation are easy to explain, It may be a good ideanamespaces is one honking great idea – let's do more than those
A) Output rows that contain "com"
grep com test.txt com complicated
b) Ignore case when matching
grep-i com test.txt comPlex ComPlex complicated
c) Output lines beginning with "complex", ignoring case
grep-i ' ^complex ' test.txt Complex is better than complicated
d) Output the line ending with "idea"
grep ' idea$ ' test.txt Idea Idea
e) Match blank line
grep-n ' ^$ ' test.txt 2 :
h) Output rows that contain "good" or "bad"
grep ' Good\|bad ' test.txt Bad Good grep-e ' Good|bad ' test.txt Bad Good idea
i) exact match word it, such words as implicit, purity, purity, etc. will not be matched
grep-w it test.txt it it It May is a good idea
j) Output lines containing "Python" and their 3 rows
grep-a 3 Python test.txt Python, by Tim petersbeautiful are better than uglyexplicit is better than implicit
Related commands
Egrep-equivalent to GREP-E
Fgrep-equivalent to Grep-f
Pgrep-equivalent to Grep-p
Linux commands-Grep:print lines matching a pattern