When you want to view specific characters in the text, we often use GREP, SED, and AWK. Today we will learn about the basic usage of the text filtering tools GREP and SED in Linux:
Regular Expression:
Definition:
It is a set of characters or special string modes.
Function: Search for text in the mode and display the lines that match the mode.
Pattern ):
Combine text characters with metacharacters of regular expressions to form matching conditions
Grep:
Grep Introduction
Grep (global search regular RE) </span> and print out the line, fully search for the regular Expression and print the row
It is a powerful text search tool that uses regular expressions to search for text and print matching rows.
Unix grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep.
Egrep is an extension of grep and supports more RE metacharacters. fgrep is fixed grep or fast grep. They treat all letters as words, that is, metacharacters in regular expressions.
It indicates its literal meaning and is no longer special. Linux uses GNU grep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.
Grep is a common and frequently-used command. Its most important function is to compare string data and display strings that meet user requirements.
It should be noted that when grep searches for a string in data, it selects data in the unit of "whole line. That is to say, if there are 10 rows in a file, two of them have
The string to be searched is displayed on the screen, and the other two rows are lost.
Note: by default, regular expressions work in greedy mode.
Grep [options] PATTERN [FILE...]
-A: searches for data in binary folders as text files.
-I: case insensitive
-- Color: match strings with colors based on reality
-V: reverse query, that is, the row that is not 'searched string' is displayed.
-O: only strings matched by the pattern are displayed.
Grep-o 'root'/etc/passwd
Root
....
-N: Output row number
-C: calculates the number of times a 'search string' is found.
-A: # match the result and display the following # rows of the matching result together
-B: # match the result and display the first # rows of the matching result together
-C: A and B show the rows before and after the matching result.
A regular expression contains some metacharacters. These characters do not represent the meaning of the character itself, but represent the meaning of the wildcard.
This section describes the cloud characters used by grep for file search.
Search for a specific string, use [] to search for a set character, any one character (.) and repeated character (*),
Characters ^ $ at the beginning and end of a line, specifying the range of duplicate characters {}, location anchor, and Group
Next we will learn the meaning of metacharacters:
^: Pin the beginning of a line
For example:
'^ Root': indicates to use root as the first line to search
$: End of the anchor row
For example:
'Nologin $ ': searches for the matched rows ending with nologin.
.: Match any single character
For example:
'R.. t': searches for rows starting with r, ending with t, and with two matching characters in the middle.
*: A single character followed by *. It matches 0 or more.
For example:
'OK *': indicates that there can be no k characters after the start of "o", or multiple times
. *: Any character
For example:
'Root. * ': searches for the rows that match any character starting with "root ".
[]: Match the Character Sequence range in []
For example:
'[A-z]' indicates matching any lowercase letter []
[^]: Match strings other []
For example:
'[^ Root]': indicates a row other than root.
^ $: Blank lines are displayed.
For example:
'^ $': Indicates to use blank rows as the mode and display matched rows.
\ <: Any string after it must appear as the word Header
For example:
'\ <Root': indicates that the query uses the root mode and must appear at the beginning of the row.
\>: Any string before it must appear at the end of the word
For example:
'Nologin \> ': indicates that the search is in root mode and must appear at the end of the row.
\ <Sting \>: returns a string and searches for text files.
For example:
'\ <Root \>': indicates that the word "root" is used for search.
\ (\): Group
For example:
\ (AB \) *: regards AB as the whole. AB can appear 0 times, once, or multiple times.
\ (\) \ 1: reference the first left brace and all content contained in the corresponding right Brace
\ 2:
\ 3:
For example:
\ (AB \) \ 1: indicates that AB is displayed as a whole and the front AB string is referenced.
\: Used to comment out the special meaning of a metacharacter. Some metacharacters in shell have special meanings.
For example:
'\/' Indicates that the search mode is '/'.
Pattern \ {m \}: Used to match the previous pattern for n times
For example:
'Root \ {2 \}: matches the root string and must appear twice
Pattern \ {m ,\}: Used to match the previous pattern at least m times.
For example, 'root \ {1, \}: indicates that at least one root error occurs.
Pattern \ {m, n \}: Used to match the previous pattern at least m times, up to n times
For example:
'Root \ {1, 2 \}: indicates that at least 1 root, at most 2 root