Regular Expressions and Linux text check tools
First, what is a regular expression?
In the simplest case, a regular expression is a set of rules and methods defined to process a large number of strings. Or in other words, regular Expressions use special characters to redefine the meaning:
For example, we use "." To represent any single character. Similar redefinition is the regular expression we are talking about;
Regular Expressions are widely referenced in the grep tool. Therefore, we first use grep to introduce what is a regular expression...
I. Three text search commands before linux Regular Expressions
Grep :( global search regular RE) fully searches regular Expressions and prints the rows)
Interpretation: The earliest text matching program uses the basic regular expression (BRE) defined by POSIX to match the text
Name: print lines matching a pattern is a powerful text search tool. It can only use basic regular expressions to search text and print matching rows.
[Root @ linux ~] # Grep 'root'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Operator: x: 11: 0: operator:/root:/sbin/nologin
[Root @ linux ~] # Format:
1) grep [OPTIONS] PATTERN [FILE...]
########## Next we will explain this document ###########
[Root @ linux ~] # Cat test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
######################################## ######################################## #########
2) grep [OPTIONS] [-e PATTERN |-f FILE] [FILE...]
Description: grep searches for corresponding rows based on the standard input "PATTERN" or named file. Matched rows are printed by default.
[Root @ linux ~] # Grep 'telphone' test.txt
My telphone number is 15648562351...
[Root @ linux ~] #
Common options:
-E: equivalent to egrep, which is specified by POSIX. With this command, you can use an extended regular expression to search for text and print the strings that meet your needs.
Note: When we use egrep, we do not need to transfer special characters, which is different from grep:
Let's take a look at the use of egrep:
123 [root @ linux ~] # Egrep 'beautiful' test.txt
This is a beautiful girl
[Root @ linux ~] #
The following are functions similar to grep-E and egrep.
[Root @ linux ~] # Grep-E '^ (a | J)'/etc/passwd
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Avahi-autoipd: x: 170: 170: Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
Abrt: x: 173: 173:/etc/abrt:/sbin/nologin
Jason: x: 1000: 1000:/home/Jason:/bin/bash-F: equivalent to fgrep, which is specified by Posix and uses a fixed string to search for text, however, regular expressions are not supported, so the command execution speed is the fastest.
[Root @ linux ~] # Grep-F 'root'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Operator: x: 11: 0: operator:/root:/sbin/nologin -- color = auto/nerver/always: color the matched text and highlight it, it is generally defined in alias;
[Root @ linux ~] # Alias
Alias cp = 'cp-I'
Alias egrep = 'egrep -- color = auto'
Alias fgrep = 'fgrep -- color = auto'
Alias grep = 'grep -- color = auto'
Alias l. = 'LS-d. * -- color = auto'
Alias ll = 'LS-l -- color = auto'
Alias ls = 'ls -- color = auto'
Alias mv = 'mv-I'
Alias rm = 'rm-I'
Alias which = 'Alias |/usr/bin/which -- tty-only -- read-alias -- show-dot -- show-tilde'
[Root @ linux ~] # [Root @ linux ~] # Grep 'home' -- color = auto/etc/passwd
Jason: x: 1000: 1000:/home/Jason:/bin/bash
[Root @ linux ~] # Grep 'home' -- color = never/etc/passwd
Jason: x: 1000: 1000:/home/Jason:/bin/bash
[Root @ linux ~] # Grep 'home' -- color = always/etc/passwd
Jason: x: 1000: 1000:/home/Jason:/bin/bash
[Root @ linux ~] #-I: Ignore the case sensitivity of characters;
[Root @ linux ~] # Cat test.txt
Good morning, zhang!
[Root @ linux ~] # Grep-I 'A' test.txt
Good morning, zhang! -O: only the matched text is displayed;
[Root @ linux ~] # Cat test.txt
Good morning, zhang!
[Root @ linux ~] # Grep-o 'zhang' test.txt
Zhang
[Root @ linux ~] #-V: -- invert-match: reverse match, matching rows out of quotation marks
[Root @ linux ~] # Cat test.txt
Good morning, zhang!
Nihao
[Root @ linux ~] # Grep-v 'good' test.txt
Nihao
[Root @ linux ~] #
# Here we can see that reverse matching is a line that does not contain 'good '-q: -- quiet, -- silient: silent mode, and no information is output;
[Root @ linux ~] # Grep-v 'good' test.txt
Nihao
[Root @ linux ~] # Grep-qv 'good' test.txt
[Root @ linux ~] #-N: displays matched rows and row numbers.
[Root @ linux ~] # Grep-n 'O' test.txt
1: Good morning, zhang!
2: nihao
[Root @ linux ~] # Grep 'O' test.txt | cat-n
1 Good morning, zhang!
2 nihao
[Root @ linux ~] #
# The n option of grep is colored, which is different from the n option of cat.-c: calculates the number of times 'pattern' is found.
[Root @ linux ~] # Grep-c 'O' test.txt
2
[Root @ linux ~] #-A: displays the n rows that match the character.
[Root @ linux ~] # Cat test.txt
GegVDFwer34fs43dfwerFG4g
GegVDFweSDFGertgg
23ere67fgSD5436fe
Nihao, zhandge
[Root @ linux ~] # Grep-A1 '23' test.txt
23ere67fgSD5436fe
Nihao, zhandge
[Root @ linux ~] #-B: displays the first n rows matching the character.
[Root @ linux ~] # Cat test.txt
GegVDFwer34fs43dfwerFG4g
GegVDFweSDFGertgg
23ere67fgSD5436fe
Nihao, zhandge
[Root @ linux ~] # Grep-B2 '23' test.txt
GegVDFwer34fs43dfwerFG4g
GegVDFweSDFGertgg
23ere67fgSD5436fe
[Root @ linux ~] #-C: displays n rows before and after the line matching the character
[Root @ linux ~] # Grep-C1 '23' test.txt
GegVDFweSDFGertgg
23ere67fgSD5436fe
Nihao, zhandge
[Root @ linux ~] #
-G: -- basic-regexp: basic regular expressions are supported;
-P: -- perl-regexp: pcre regular expression is supported;
-E: PATTERN, -- regexp = PATTERN: multi-mode mechanism;
-F: FILE, -- file = FILE: each line contains a pattern text FILE, that is, grep script;
We will not describe the two below. There are related examples above.
Egrep: Extended grep, which uses an extended regular expression (ERE) to match text.
The egrep command is equivalent to grep-E. With this command, you can use an extended regular expression to search for text and print the strings that meet your needs.
Fgrep: Quick grep. This version matches fixed strings instead of regular expressions. It is the only version that can match multiple strings in parallel.
The fgrep command is equivalent to grep-F. It uses a fixed string to search for text, but does not support regular expression reference. Therefore, this command can be executed at the fastest speed.
Linux basics tutorial ---- Regular Expression Basics
Linux Regular Expression sed details
Linux regular expression features and differences between BRE and ERE
Grep uses concise and Regular Expressions
Regular Expression usage
Assertion with Zero Width of a regular expression
Regular Expressions and file formatting commands in Linux (awk/grep/sed)
Basic Regular Expression
Regular Expressions
Ii. Basic regular expression:
Basic meaning: A string consisting of some basic characters and some special characters that can be easily searched and matched with text with certain syntax rules.
Category: Basic Regular Expressions and extended Regular Expressions
1) metacharacters of the basic Regular Expression
What is metacharacters?
Metacharacters are one or more characters that replace one or more characters. They are actually the following types.
1) character matching
.: Match any single character
[Root @ linux ~] # Grep 'R.. t'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Operator: x: 11: 0: operator:/root:/sbin/nologin
Ftp: x: 14: 50: FTP User:/var/ftp:/sbin/nologin
[Root @ linux ~] # Note the following example:
1234567 [root @ linux ~] # Grep '. 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
In this way, all rows are matched.
[]: Match a single character in the specified range:
[Root @ linux ~] # Grep '[aj] H' test002.txt
Ahjhb
[Root @ linux ~] # [^]: Match a single character in a specified range
[Root @ linux ~] # Grep '[^ a] H' test002.txt
Ahjhb
[Root @ linux ~] # [: Alnum:]: Numbers and uppercase/lowercase letters --> "A-Za-z0-9"
[Root @ linux ~] # Grep '[[: alnum:] 'test002.txt
B
AB
Acb
AaX2Ab
A \ [Ah \? Jhb
Aba1baba5bab
[Root @ linux ~] #
#### The following example is no longer provided. It is very simple: [: digit:]: digit character ------------> "0-9"
[Root @ linux ~] # Grep '[[: digit:] 'test.txt
My telphone number is 15648562351...
[Root @ linux ~] # Matching the phone number
[: Punct:]: punctuation character ----------> "?., "
1234567 [root @ linux ~] # Grep '[[: punct:] 'test.txt
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
[Root @ linux ~] # Match all punctuation marks
[: Alpha:]: letter character ------------> "A-Za-z"
12345678 [root @ linux ~] # Grep '[[: alpha:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
Are all letters excluded?
[: Graph:]: All buttons except space character (Space key and Tab key)
[Root @ linux ~] # Grep '[[: graph:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
[Root @ linux ~] # Check the source files and compare them, right?
[: Space:]: blank characters, including space key [Tab]
[Root @ linux ~] # Grep '[[: graph:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
[Root @ linux ~] # This demo is not very effective. You can try "grep '[^ [: space:] 'test.txt"
[: Blank:]: represents the Space key and the [Tab] key.
[: Lower:]: lowercase letter character ----------> "a-z"
[Root @ linux ~] # Grep '[[: lower:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you?
[: Upper:]: uppercase letter character ----------> "A-Z"
[Root @ linux ~] # Grep '[^ [: lower:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
Can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you? Is this true?
[Root @ linux ~] # Grep '[[: upper:] 'test.txt
This is a beautiful girl
So do you want to know who is her?
Oh! I can't tell you?
My telphone number is 15648562351...
Beat wish to you?
[: Cntrl:]: indicates that the control buttons on the keyboard include "CR, LF, Tab, Del"
[: Print:]: Indicates characters that can be printed.
[: Xdigit:]: represents the hexadecimal number type-> "0-9, A-F, a-f"
The above three are not frequently used and will not be demonstrated.
For more details, please continue to read the highlights on the next page: