Linux operation _grep/egrep tool usage

1. Introduction to grep command

Command format: grep [-cinvABC] 'word' filename , common options are as follows:

  • -c: Indicates printing the number of lines that meet the requirements.
  • -i: Indicates that case is ignored.
  • -n: Indicates output lines and line numbers that meet the requirements.
  • -v: Indicates to print lines that do not meet the requirements.
  • -A: followed by a number (with or without spaces), for example -A2 means to print the line that meets the requirements and the following two lines.
  • -B: followed by a number, for example -B2 means to print the line that meets the requirements and the two lines above.
  • -C: followed by a number, for example -C2 means to print the line that meets the requirements and two lines above and below.

 

2. Filter out the lines with a certain keyword and output the line number

Explanation: The preceding numbers are displayed in green, indicating line numbers.

 

3. Filter out the lines without a certain keyword and output the line number

 

Fourth, filter out all rows containing numbers

Description: As long as there is a number, it is matched.

 

5. Filter out all rows that do not contain numbers

Description: As long as it contains a number, it will not be displayed.

 

6. Filter out all lines starting with #

Note: This contains blank lines.

 

Seven, filter out all blank lines and lines starting with #

In regular expressions, ^ indicates the start of a line, $ indicates the end of a line, and ^$ indicates an empty line.

 

How to print out lines that don't start with an English letter? An example is as follows:

Description: If you want to filter numbers, use the form [0-9] (when a form like [15] is encountered, it only contains 1 or 5). If you want to filter numbers and uppercase and lowercase letters, write it like [0-9a-zA-Z]. Also, [^ character] means characters other than the characters within [ ] .

Note: There is a difference between writing ^ inside and outside square brackets.

 

Eight, filter out any character and repeated characters

. represents any character. In the above example, ro means to filter out lines with an arbitrary character between r and o.

* means zero or more characters preceding *. In the above example, ooo* means oo, ooo, oooo... or more o's.

In the above example, .* means zero or more arbitrary characters, including blank lines, which will match all lines in the /etc/passwd file.

 

Nine, specify the number of characters to be filtered out

Description: The symbol { }, which is a number inside, indicates the number of times the preceding character is to be repeated .

Note (emphasis added): The escape character \ should be added around { }. In addition, the use of "{ }" can also represent a range, the specific format is {n1,n2}, where n1 < n2, which means repeating the preceding characters from n1 to n2 times, and n2 can also be empty, which means greater than or equal to n1 times .

 

10. Filter out one or more specified characters (start using egrep command)

Note: The egrep command uses the symbol +, which means to match one or more characters before +. This "+" does not support the direct use of the grep command; { } can be used directly by egrep without adding \ escape. E.g:

 

11. Filter out zero or one specified character

 

12. Filter out string 1 or string 2

 

Thirteen, the use of ( ) in egrep

Description: Here ( ) is used to represent a whole. In the above example, the lines containing rooo or rato will be filtered out.

 

Another note: You can also combine ( ) with other symbols, for example:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325096507&siteId=291194637