Home  >  Article  >  Operation and Maintenance  >  What is the usage of grep in linux

What is the usage of grep in linux

WBOY
WBOYOriginal
2021-12-27 13:46:3623610browse

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.

What is the usage of grep in linux

#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][范本样式][文件或目录...]

Parameters:

  • -a or --text: Do not ignore binary data.

  • -A093d8b15577fdaaa0449ecb47f192d96 or --after-context=093d8b15577fdaaa0449ecb47f192d96 : 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.

  • -B3a17719c3dc5c6a30ddb351d98fd6bff or --before-context=3a17719c3dc5c6a30ddb351d98fd6bff : 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.

  • -C3a17719c3dc5c6a30ddb351d98fd6bff or --context=3a17719c3dc5c6a30ddb351d98fd6bff or -3a17719c3dc5c6a30ddb351d98fd6bff : In addition to displaying the line that matches the style outside, and displays the content before and after the line.

  • ##-d 26e39fa7e1b26005f564b20d47361531 or --directories=26e39fa7e1b26005f564b20d47361531 : 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.

  • -e144db954177ff6cdba977c2cd2085ab1 or --regexp=144db954177ff6cdba977c2cd2085ab1 : Specify a string as the style for searching file content.

  • -E or --extended-regexp : Use extended regular expression style.

  • -f02c041fdf3648d9b505fb98f4d11722d or --file=02c041fdf3648d9b505fb98f4d11722d : 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

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字符的行

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn