Learn a little shell every day: Linux Three Musketeers-grep command

Preface

Linux's "Three Musketeers" grepmeans: sed, awk, .
The reason why it is called the Three Musketeers is that the above tools can better process the query results of Linux.

Regular expression

The above commands are called Three Musketeers because they can combine regular expressions to process query content, and only the above “Three Musketeers” can be used in combination with regular expressions. The following describes the meaning of the following regular expression characters:

Metacharacter Features Description
^ Match the first line Means start with a certain character
$ Match end of line Means ending with a character
^$ The meaning of blank lines Means blank line
. Match any single character Means to match any single character
* Match 0 or more of this character Represents any number of repeated characters
\ Escape character Indicates escape character
[] Match characters in brackets Means to filter the characters in brackets
.* Represents any number of characters Means to match any number of characters

The special ones are as follows:

Regular expression Explanation
. Any character.
[abc] Means to match a character, this character must be one of abc.
[a-zA-Z] Means to match a character, this character must be one of the 52 letters of az or AZ.
[^123] Match a character, this character is all characters except 1, 2, 3.

For some commonly used character sets, the system defines:

character set Equivalent system definition
[A-Za-z] Equivalent to [[:alpha:]]
[0-9] Equivalent to [[:digit:]]
[A-Za-z0-9] Equivalent to [[:alnum:]
tab,space Waiting for blank characters [[:space:]]
[A-Z] Equivalent to [[:upper:]]
[a-z] Equivalent to [[:lower:]]
Punctuation [[:point:]]

Matches:

\{m\} Match the preceding character m times
\{m,\} Match the preceding character at least m times
\{m,n\} Match the character before it at least m times and at most n times
\? Match the content that appears before it 0 or 1 times, equivalent to {0,1}
* Matching the content that appears before it any number of times is equivalent to {0,}, so ".*" expresses any character any number of times, that is, it matches all the content no matter what.

The Three Musketeers is very powerful, but we only need to master the field they were good at: grep擅长查找功能, sed擅长取行和替换, awk擅长取列.

grep command usage

usage:

grep [选项]... PATTERN [FILE]...

List some common option commands

Choose option Explanation
–color Color the matched text
-v Conversely (invert), display the matched lines without patern
-i Ignore case (ignore case)
-n Show matching line numbers
-c Count the number of matched rows
-The Only display strings matched by the pattern
-q Silent mode, no information is output
-A --After-context=NUM print NUM lines ending with text
-B --Before-context=NUM print NUM lines starting with text
-C --Context=NUM print out NUM lines of text
-e Realize the logical or relationship between multiple options, for example: grep -e'cat' -e'dog' file
-w The matched text can only be a word, not a certain part of the word. For example, if there is liker in the text, and I searched for just like, you can use the -w option to avoid matching liker
-E Open extended (Extend) regular expression, using regular is equivalent to egrep
-F Does not support regularity, which is equivalent to fgrep

grep command example

Usage 1:

[root@hadoop-master test-grep]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -vc "root" /etc/passwd
43
[root@hadoop-master test-grep]# grep -o "root" /etc/passwd
root
root
root
root

Usage 2:

[root@hadoop-master test-grep]# grep -A 2 "core id" /proc/cpuinfo
core id		: 0
cpu cores	: 1
apicid		: 0
[root@hadoop-master test-grep]# grep -B 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
[root@hadoop-master test-grep]# grep -C 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0

Usage three:

[root@hadoop-master test-grep]# grep "/.*sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
setroubleshoot:x:990:986::/var/lib/setroubleshoot:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

Usage four:

[root@hadoop-master test-grep]# grep "/.\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

Usage five:

[root@hadoop-master test-grep]# grep -w ".\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

Usage six:
p1

Guess you like

Origin blog.csdn.net/u011047968/article/details/108720525