Use the grep command in linux

  • 2021-01-03 21:14:47
  • OfStack

Linux grep command

The Linux grep command is used to find qualified strings in a file. It can also be used to find files that contain the specified template style. It can use a regular expression search to search for the specified string pattern in a file, list the filename that contains a string of matching pattern subcharacters, and output a line of text that contains that string.

Here's how grep works: It searches for string templates 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 search results are sent to standard output without affecting the contents of the original file.

Basic grammar:


grep [options] pattern [files]

Main parameters of [options] :

-ES18en or --text: Do not ignore data in base 2.

-A < According to the number of rows > Or - after - context = < According to the number of rows > : Displays the contents of that column, in addition to the one that fits the template style.

-ES31en or -- ES32en-ES33en: Identifies the first character of the line before displaying the line that matches the style.

-B < According to the number of rows > Or - before - context = < According to the number of rows > : Displays the preceding line except for the one that fits the style.

-ES45en or --count: Counts the number of columns that fit the style.

-C < According to the number of rows > Or - context = < According to the number of rows > Or - < According to the number of rows > : Displays everything before and after that line, except the one that fits the style.

-d < action > Or - directories = < action > : This parameter must be used when specifying that you are looking for a directory and not a file, otherwise the grep directive returns the information and stops the action.

-e < Sample style > Or - regexp = < Sample style > : Specifies the string as the style to find the file contents.

-ES76en or -- ES77en-ES78en: To use styles as extended common representations.

-f < Rules file > Or - file = < Rules file > : Specifies a rule file whose contents contain one or more rule styles, and lets grep find the file contents that match the rule conditions in a format of 1 rule style per line.

-ES90en or -- ES91en-ES92en: A list of styles that are treated as fixed strings.

-ES95en or -- ES96en-ES97en: Use styles as normal representations.

-ES100en or -- ES101en-ES102en: Do not identify the file name to which the line matching the style belongs until the line 1 is displayed.

-ES105en or -- ES106en-ES107en: Indicates the file name to which the line matching the style belongs before the line 1 is displayed.

-ES110en or -- ignore-ES112en: Ignores character case differences.

-ES115en or -- file-ES117en-ES118en: Lists the name of the file whose contents match the specified style.

-ES121en or -- files-ES123en-ES124en: Lists file names whose file contents do not match the specified style.

-ES127en or -- ES128en-ES129en: Identifies the number of columns in the row before displaying the row 1 that fits the style.

-ES132en or -- ES133en-ES134en: Displays only the matching PATTERN part.

-ES138en or --quiet or --silent: Does not display any information.

-ES143en or --recursive: This parameter has the same effect as specifying the "-ES145en recurse" parameter.

-ES149en or -- ES150en-ES151en: No error message displayed.

-ES154en or -- ES155en-ES156en: Displays all lines that do not contain matching text.

-ES159en or --version: Displays version information.

-ES163en or -- ES164en-ES165en: Displays only columns that match the full word.

-ES168en -- ES169en-ES170en: Displays only columns with full columns.

-ES173en: This parameter has the same effect as the specified "-ES174en" parameter.

- the & # 63; : Displays the matching rows at the same time? Rows such as ES177en-2 pattern filename display both the top and the bottom of the matching row.

Main parameters of pattern regular expression:

\: Ignores the original meaning of special characters in regular expressions.

^: Matches the beginning line of the regular expression.

$: Matches the end line of the regular expression.

\ < : Starts with the line that matches the regular expression.

\ > : to the end of the line that matches the regular expression.

[] : A single character, such as [A] or A, meets the requirements.

[-]: Scopes such as [ES203en-ES204en], i.e. A, B, C1 up to Z meet the requirements.

. : All single characters.

* : Has the character, the length may be 0.

Code examples:

Example 1: In the current directory, look for the file that contains the string "test" in a file prefixed with "test" and print out the line of the string, using the following command:


grep test test*

Output:


testfile1:This a Linux testfile! # list testfile1  The file contains test Character of the line  

testfile_2:This is a linux testfile! # list testfile_2  The file contains test Character of the line  

testfile_2:Linux test # list testfile_2  The file contains test Character of the line 

Example 2: Reverse lookup, using the "-v" parameter to print out the contents of the ineligible rows. Find lines that do not contain test in a file whose file name contains test


grep -v test *test*

Output:


testfile1:helLinux! 

testfile1:Linis a free Unix-type operating system. 

testfile1:Lin 

testfile_1:HELLO LINUX! 

testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. 

testfile_1:THIS IS A LINUX TESTFILE! 

testfile_2:HELLO LINUX! 

testfile_2:Linux is a free unix-type opterating system.


Related articles: