Linux command: grep

grep

1. Role
grep command to search for specific contents of the specified file, and outputs the row containing the standard content. grep stands for Global Regular Expression Print, represents the global regular expression version, its usage rights for all users.

2. Format

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

3. The main parameters

  • -a or --text  : Do not ignore the binary data.
  • -A <the number of lines> or --after-context = <number of lines displayed>  : In addition to displaying the template in line with the row outside the pattern, and then display the contents of the row.
  • -b-offset or --byte  : Prior to demonstrate compliance with the line style, mark the number of the first character of the line.
  • -B <the number of lines> or --before-context = <number of lines displayed>  : In addition to displaying the line patterns meet outside, and displays the contents of the previous row.
  • -c or --count  : calculated in line with the number of columns style.
  • -C <the number of lines> or --context = <number of lines displayed> or - <the number of lines>  : In addition to displaying the line style than meet, and the display contents before the row.
  • -d <action> or --directories = <action>  : When you specify when you want to find is a directory rather than a file, you must use this parameter, otherwise the grep command will return information and stop action.
  • -e <style templates> or --regexp = <template pattern>  : find files specified string as the contents of the style.
  • Or -E-regexp --extended  : the style is common notation used extending.
  • -f <rule file> or --file = <rule file>  : specify rules file, the contents of which contain one or more regular pattern, so grep to find content file rule conditions are met, the format for each line of a regular pattern.
  • -F regexp-or --fixed  : a style seen as a list of fixed strings.
  • -G-regexp or --basic  : the style of notation as ordinary use.
  • -h-filename or --no  : Prior to demonstrate compliance with the line style, does not indicate the file name of the row belongs.
  • -H or-filename --with  : Prior to demonstrate compliance with the line style, it represents a file name of the row belongs.
  • Case--i or --ignore  : ignore character case differences.
  • -l-with-The matches or --file  : lists the contents of the file file name matches the specified style.
  • -L-the without-match or --files  : lists the contents of the file file name does not comply with the specified style.
  • Number The -n-or --line  : Prior to demonstrate compliance with the line style, mark the number of columns in the row number.
  • -o-matching or --only  : PATTERN only matching portion.
  • -q or --quiet or --silent  : do not display any information.
  • --recursive or -r  : effect of this parameter is specified and "-d recurse" the same parameters.
  • -s-messages or --no  : no error message.
  • -v-match or --revert  : Displays all rows matching text.
  • -V or --version  : show the version information.
  • -w-regexp or --word  : show only matching whole-word column.
  • --line-regexp -x  : display only full compliance with columns columns.
  • -Y  : effect of this parameter is specified and "-i" the same parameters.

4. Application Examples

(1) in the current directory, find the file suffix has the word test is included in a string of test files, and print out the string line. In this case, the following command:

grep test *test

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

(2) recursively find qualified files. For example, to find the specified directory / etc / acpi and its subdirectories (subdirectories if one exists) all the files that contain the string "update" file, and print out the contents of the string row, the command is:

grep -r update /etc/acpi 

Output:

$ 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 lookup. Each of the previous example is to find and print out the qualifying rows by "-v" argument can print out the contents do not meet the conditions of the line.

Find a line in the file filename contains test is not included in the test, this time using the command:

grep -v test *test*   #“-v test”表示查找不包含test的行,“*test*”表示包含test文件名的文件

The results are 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.

 

Guess you like

Origin blog.csdn.net/q1449516487/article/details/92411968