In Linux, the grep command is used to find qualified strings in files. The syntax is "grep option pattern file"; this command can find files containing the specified template style. If the file content matches the specified template style, the column containing the template style will be displayed.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
What is the usage of grep in Linux
The Linux grep command is used to find strings that meet the conditions in the file.
The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or if - is given, the grep command reads data from the standard input device.
Syntax
grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]
Copy after login
Parameters:
-a or --text: Do not ignore binary data.
-A or --after-context= : In addition to displaying the column that conforms to the template style, and display this row What follows.
-b or --byte-offset: Before displaying the line that matches the style, indicate the number of the first character of the line.
-B or --before-context= : In addition to displaying the line that conforms to the style, and displaying the line before Content.
-c or --count : Count the number of columns that match the style.
-C or --context= or - : In addition to displaying the line that matches the style outside, and displays the content before and after the line.
- ##-d or --directories= : This parameter must be used when specifying directories rather than files to be searched, otherwise grep The command will report the information and stop the action.
- -e or --regexp= : Specify a string as the style for searching file content.
- -E or --extended-regexp : Use extended regular expression style.
- -f or --file= : Specify a rule file whose content contains one or more rule patterns, allowing grep to search for rules that match the rules The file content of the condition, in the format of one rule pattern per line.
- -F or --fixed-regexp : Treat styles as a list of fixed strings.
- -G or --basic-regexp : Treat the style as a normal notation.
- -h or --no-filename: Do not indicate the file name to which the line belongs before displaying the line that matches the style.
- -H or --with-filename: Before displaying the line that matches the style, indicates the file name to which the line belongs.
- -i or --ignore-case : Ignore differences in character case.
- -l or --file-with-matches: List file names whose content matches the specified pattern.
- -L or --files-without-match : List file names whose content does not match the specified style.
- -n or --line-number: Before displaying the row that matches the style, indicate the column number of the row.
- -o or --only-matching: Only display the matching PATTERN part.
- -q or --quiet or --silent : No information is displayed.
- -r or --recursive: The effect of this parameter is the same as specifying the "-d recurse" parameter.
- -s or --no-messages : Do not display error messages.
- -v or --invert-match : Display all lines that do not contain matching text.
- -V or --version : Display version information.
- -w or --word-regexp: Only display columns that match whole words.
- -x --line-regexp: Only display the columns that match all columns.
- -y: The effect of this parameter is the same as specifying the "-i" parameter.
The example is as follows:
In the current directory, find the file containing the test string in the file with the word file suffixed, and print out the string OK. At this time, you can use the following command:
grep test *file
Copy after login
The result is as follows:
$ grep test test* #查找前缀有“test”的文件包含“test”字符串的文件
testfile1:This a Linux testfile! #列出testfile1 文件中包含test字符的行
testfile_2:This is a linux testfile! #列出testfile_2 文件中包含test字符的行
testfile_2:Linux test #列出testfile_2 文件中包含test字符的行
Copy after login
Related recommendations: "
Linux Video Tutorial"
The above is the detailed content of What is the usage of grep in linux. For more information, please follow other related articles on the PHP Chinese website!