Linux grep command

 

The Linux grep command is used to find qualified strings in a 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 conform to the specified template style, the default grep command will display the column containing the template style. If no filename is specified, or - is given, the grep command reads data from the standard input device.

grammar

grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

Parameters :

  • -a or --text  : Do not ignore binary data.
  • -A<display line number> or --after-context=<display line number>  : In addition to displaying the column that conforms to the template style, and display the content after the line.
  • -b or --byte-offset  : Before displaying the line that matches the style, mark the number of the first character of the line.
  • -B<display line number> or --before-context=<display line number>  : In addition to displaying the line that conforms to the style, and display the content before the line.
  • -c or --count  : Count the number of columns matching the pattern.
  • -C<display line number> or --context=<display line number> or -<display line number>  : In addition to displaying the line that conforms to the style, and display the content before and after the line.
  • -d <action> or --directories=<action>  : This parameter must be used when specifying that the directory to be searched is not a file, otherwise the grep command will report information and stop the action.
  • -e<template-style> or --regexp=<template-style>  : Specify a string as the style for finding file content.
  • -E or --extended-regexp  : Use the style as an extended regular expression.
  • -f<rule file> or --file=<rule file>  : Specify a rule file, its content contains one or more rule styles, let grep find the file content that meets the rule conditions, the format is one rule style per line.
  • -F or --fixed-regexp  : Treat styles as a list of fixed strings.
  • -G or --basic-regexp  : Treat styles as normal notation to use.
  • -h or --no-filename  : Do not indicate the name of the file 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, the name of the file to which the line belongs.
  • -i or --ignore-case  : Ignore differences in character case.
  • -l or --file-with-matches  : List file names whose contents match the specified style.
  • -L or --files-without-match  : List file names whose contents do not match the specified pattern.
  • -n or --line-number  : Before displaying the line that matches the style, mark the column number of the line.
  • -o or --only-matching  : Show only matching PATTERN parts.
  • -q or --quiet or --silent  : Do not display any information.
  • -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  : Show only whole-word columns.
  • -x --line-regexp  : Only show columns that match all columns.
  • -y  : The effect of this parameter is the same as specifying the "-i" parameter.

Linux Command Encyclopedia Linux Command Encyclopedia

example

1. In the current directory, find the file containing the test string in the file suffixed with the word file, and print out the line of the string. At this point, you can use the following command:

grep test *file 

The result looks like this:

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

2. Find files that meet the conditions recursively. For example, to find files containing the string "update" in all files in the specified directory /etc/acpi and its subdirectories (if there are subdirectories), and print out the content of the line where the string is located, the command used is:

grep -r update /etc/acpi 

The output 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 

3. Reverse search. The previous examples are to find and print out the lines that meet the conditions. The "-v" parameter can print out the content of the lines that do not meet the conditions.

Find the line that does not contain test in the file whose file name contains test. In this case, the command used is:

grep -v test *test*

The result looks like this:

$ 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.  

Guess you like

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