The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.
grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to standard output without affecting the contents of the original file.
grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can do some automated text processing with these return values
Linux Musketeers old Three, filter required content, v to exclude content, followed by content to be excluded
Example: Grep-v oldboy test.txt
grep 20-a ett.txt show 10 rows including 20 20-30
-B In addition to displaying a matching row, and displaying the num rows before the line
-a displays the NUM row after the line, except for a matching row
-C In addition to displaying a matching line and displaying the num rows before and after the line
--color=auto matching characters plus color for filtering
-n means the line number is printed on the matched content
2. Command function:
A specific character used for filtering/searching. Can use regular expressions can be used with a variety of commands, the use of very flexible
3. Command parameters:
-a< Show rows >--after-context=< show rows > #除了显示符合范本样式的那一列之外 and display the contents after that line
-b< Displays rows >--before-context=< displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.
-c< Show rows >--context=< show rows > or-< show rows > #除了显示符合样式的那一行之外 and display the front and back of the row
-I.--ignore-case #忽略字符大小写的差别.
-N--line-number #在显示符合样式的那一行之前, indicating the number of columns in the row.
-V excludes the content, followed by the content to be excluded, and displays all lines that do not contain matching text.
Regular Expressions for 4.grep:
^ #锚定行的开始 such as: ' ^grep ' matches all lines that begin with grep.
$ #锚定行的结束 such as: ' grep$ ' matches all lines that end with grep.
. #匹配一个非换行符的字符 such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.
* #匹配零个或多个先前字符 such as: ' *grep ' matches all one or more spaces followed by the line of grep.
. * #一起用代表任意字符.
[] #匹配一个指定范围内的字符, such as ' [Gg]rep ' matches grep and grep.
[^] #匹配一个不在指定范围内的字符, such as: ' [^a-fh-z]rep ' matches the beginning of a letter that does not contain a-r and t-z, immediately following the line of the Rep.
\(.. \) #标记匹配字符, such as ' \ (love\) ', Love is marked as 1.
\< #锚定单词的开始, such as: ' \<grep ' matches lines that contain words that begin with grep.
\> #锚定单词的结束, such as ' grep\> ', matches lines that contain words ending with grep.
X\{m\} #重复字符x, M times, such as: ' 0\{5\} ' matches rows containing 5 O.
X\{m,\} #重复字符x, at least m times, such as: ' O\{5,\} ' matches rows with at least 5 O.
X\{m,n\} #重复字符x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows of 5--10 O.
\w #匹配文字和数字字符, that is, [a-za-z0-9], such as: ' G\w*p ' is matched with a G followed by 0 or more literal or numeric characters, followed by P.
\w #\w The reverse form, matching one or more non-word characters, such as the dot period, and so on.
\b #单词锁定符, such as: ' \bgrep\b ' only matches grep.
5. Usage Examples:
Instance one: Find the specified process
Ps-ef|grep SVN
[Email protected] data]# Ps-ef|grep SVN
Root 2861 2656 0 04:45 pts/0 00:00:00 grep svn
Example two: Find the number of specified processes,
Ps-ef|grep svn-c
Ps-ef|grep-c SVN
[Email protected] data]# ps-ef|grep-c SVN
1
[Email protected] data]# Ps-ef|grep svn-c
1
Example 3: Search by reading keywords from a file
Command:
Cat Test.txt | Grep-f Test2.txt
Output:
[email protected] test]# cat Test.txt
Hnlinux
Peida.cnblogs.com
Ubuntu
Ubuntu Linux
Redhat
Redhat
LinuxMint
[email protected] test]# cat Test2.txt
Linux
Redhat
[email protected] test]# Cat Test.txt | Grep-f Test2.txt
Hnlinux
Ubuntu Linux
Redhat
LinuxMint
[Email protected] test]#
Description
The output Test.txt file contains the content lines of the keywords read out from the Test2.txt file
Example 3: Reading a keyword from a file to search for and displaying line numbers
Command:
Cat Test.txt | GREP-NF Test2.txt
Output:
[email protected] test]# cat Test.txt
Hnlinux
Peida.cnblogs.com
Ubuntu
Ubuntu Linux
Redhat
Redhat
LinuxMint
[email protected] test]# cat Test2.txt
Linux
Redhat
[email protected] test]# Cat Test.txt | GREP-NF Test2.txt
1:hnlinux
4:ubuntu Linux
6:redhat
7:linuxmint
[Email protected] test]#
Description
The output Test.txt file contains the line of contents of the keyword read from the test2.txt file and displays the line number of each line
Example five: Find keywords from a file
[email protected] data]# Cat B
I am a man
I am a woman
I am a girl
[[email protected] data]# grep ' man ' b
I am a man
I am a woman
Example six: Find keywords from multiple files
[email protected] data]# Cat A
You aren't man
[email protected] data]# Cat B
I am a man
I am a woman
I am a girl
[[email protected] data]# grep ' man ' a B
A:You is not mans
B:i am a man
B:i am a woman
Example seven: Finding line content starting with I
[email protected] data]# Cat B | grep ' ^i '
I am a man
I am a woman
I am a girl
Example eight: Finding line content that starts with a non-I
[email protected] data]# Cat B | grep ^[^i]
Example nine: Output line content ending in n
[email protected] data]# cat b|grep ' n$ '
I am a man
I am a woman
Instance Ten: Displays a line of content containing an ED or at character
Command:
Cat Test.txt |grep-e "Ed|at"
Example 11: Displays all rows in the current directory that contain at least 7 consecutive lowercase characters for each string in a file ending in. txt
Command:
grep ' [a-z]\{7\} ' *.txt
Linux Basic command grep