Linux grep command

Linux 命令大全Linux Command Encyclopedia

Linux grep (global regular expression) command is used to search for strings or regular expressions that match the criteria in files.

The grep command is used to search for files whose contents contain a specified pattern. If a file's content matches the specified pattern, by default the grep command will display the line containing that pattern. If no file name is specified, or if the given file name is-, then the grep command will read data from the standard input device.

Syntax

grep [options] pattern [files]
或
grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]
  • pattern - Indicates the string or regular expression to search for.
  • files - Indicates the file names to search; multiple files can be searched at the same time. If the files parameter is omitted, data is read from standard input by default.

Common options::

  • -i: Ignore case when matching.
  • -v: Reverse search, only print lines that do not match.
  • -n: Display the line numbers of matching lines.
  • -r: Recursively search for files in subdirectories.
  • -l: Only print the names of matching files.
  • -c: Only print the number of matching lines.

More parameter descriptions:

  • -a or --text: Do not ignore binary data.
  • -A<number of lines to display> or --after-context=<number of lines to display>: In addition to displaying the line that matches the pattern, also display the content after that line.
  • -b or --byte-offset: Before displaying the line that matches the pattern, indicate the number of the first character in that line.
  • -B<number of lines to display> or --before-context=<number of lines to display>: In addition to displaying the line that matches the pattern, also display the content before that line.
  • -c or --count: Count the number of lines that match the pattern.
  • -C<number of lines to display> or --context=<number of lines to display> or -<number of lines to display>: In addition to displaying the line that matches the pattern, also display the content before and after that line.
  • -d <action> or --directories=<action>: When the specified target to search is a directory rather than a file, this parameter must be used; otherwise, the grep command will report a message and stop.
  • -e<pattern> or --regexp=<pattern>: Specify a string as the pattern for searching file contents.
  • -E or --extended-regexp: Treat the pattern as an extended regular expression.
  • -f<rule file> or --file=<rule file>: Specify a rule file whose content contains one or more rule patterns, allowing grep to search file contents that meet the rule criteria. The format is one rule pattern per line.
  • -F or --fixed-regexp: Treat the pattern as a list of fixed strings.
  • -G or --basic-regexp: Treat the pattern as an ordinary regular expression.
  • -h or --no-filename: Before displaying the line that matches the pattern, do not indicate the file name to which the line belongs.
  • -H or --with-filename: Before displaying the line that matches the pattern, indicate the file name to which the line belongs.
  • -i or --ignore-case: Ignore differences in character case.
  • -l or --file-with-matches: List the names of files whose contents match the specified pattern.
  • -L or --files-without-match: List the names of files whose contents do not match the specified pattern.
  • -n or --line-number: Before displaying the line that matches the pattern, indicate the line number of that line.
  • -o or --only-matching: Only display the part that matches PATTERN.
  • -q or --quiet or --silent: Do not display any information.
  • -r or --recursive: This parameter has the same effect 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 the matching text.
  • -V or --version: Display version information.
  • -w or --word-regexp: Only display lines that match whole words.
  • -x --line-regexp: Only display lines that match the entire line.
  • -y : This parameter has the same effect as specifying the "-i" parameter.

Linux 命令大全Linux Command Encyclopedia

Examples

1. Search for the string "hello" in the file file.txt, and print the matching lines:

grep hello file.txt

2. Recursively search all files in directory dir for lines matching the regular expression "pattern", and print the file names and line numbers of the matching lines:

grep -r -n pattern dir/

3. Search for the string "world" in standard input, and only print the number of matching lines:

echo "hello world" | grep -c world

4. In the current directory, search files with the suffix "file" in their names for files containing the string "test", and print the lines containing that string. In this case, 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字符的行 

5. Recursively search for files that meet the criteria. For example, to find files containing the string "update" in all files under the specified directory /etc/acpi and its subdirectories (if any), and print the contents of the lines containing that string, use the following command:

grep -r update /etc/acpi 

The output result is as follows:

$ grep -r update /etc/acpi #以递归的方式查找“etc/acpi”  
#下包含“update”的文件  
/etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.)  
Rather than  
/etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of  
IO.) Rather than  
/etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update 

6. Reverse search. The previous examples search for and print the lines that meet the criteria. Using the "-v" parameter can print the contents of lines that do not meet the criteria.

To find lines that do not contain "test" in files whose file names contain "test", use the following command:

grep -v test *test*

The result is as follows:

$ grep-v test* #查找文件名中包含test 的文件中不包含test 的行  
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.  
Other extensions