Latest web development tutorials

Linux grep command

Linux command Daquan Linux command Daquan

Linux grep command is used to find files that match the criteria string.

grep command is used to find content that contains the specified template style file, if you find the content of a file conforms to the specified template style, the default grep command that will contain the style template columns displayed. If not specify any file name, file name, or given "-" the grep command will read data from the standard input device.

grammar

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

Parameters:

  • -a --text not ignore or binary data.
  • -A <Display Columns> or --after-context = <Display Columns> In addition to displaying the row in line outside the template style, and displays the contents of the column later.
  • -b or --byte-offset before displaying the row in line with the template styles, and identify the row number of the first character position.
  • -B <Display Columns> or --before-context = <Display Columns> In addition to displaying the row in line outside the template style, and displays the contents of the column before.
  • -c or --count calculated in line with the number of columns template styles.
  • -C <Display Columns> or --context = <Display Columns> or - <Display Columns> In addition to displaying the row in line outside the template style, and displays the contents of the column before after.
  • -d <operates> or --directories = <operates> When you want to find the specified file is a directory instead, you must use this parameter, otherwise the grep command will return information and stop action.
  • -e <style template> or --regexp = <template style> Find the file contents as specified string style template.
  • -E Or --extended-regexp the template style for extending the general notation used.
  • -f <template file> or --file = <template file> Specifies the template file, the contents of which contain one or more template styles, so grep to find content template files that meet the conditions of the format for each column a template style.
  • -F Or --fixed-regexp will be treated as a template style list of fixed strings.
  • -G Or --basic-regexp will be treated as ordinary template style notation to use.
  • -h or --no-filename before displaying the row in line with the template styles, do not indicate the file name of the column belongs.
  • -H Or --with-filename before displaying the row in line with the template styles, file name indicates that the column belongs.
  • Difference -i or --ignore-case ignore character case.
  • -l or --file-with-matches lists the contents of the documents that match the specified template-style file names.
  • -L Or --files-without-match list file contents do not match the specified template-style file names.
  • -n or --line-number before displaying the row in line with the style template, mark the number of columns number of the column.
  • -q or --quiet or --silent not display any information.
  • Effects and specify -r or --recursive this parameter is the same as "-d recurse" parameter.
  • -s or --no-messages does not display an error message.
  • -v or --revert-match reverse lookup.
  • -V Or --version display version information.
  • -w or --word-regexp displays only whole-word matching columns.
  • -x or --line-regexp display only rows that match the whole column.
  • -y effect of this parameter and specify "-i" the same parameters.
  • --help online help.

Linux command Daquan Linux command Daquan

Examples

1, in the current directory, find suffix have "test" word file contains the file "test" string, and print out the line of the string. In this case, you can use the following command:

grep test *file 

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 (if there is a subdirectory, then) all the files that contain the string "update" file, and print out the contents of the string the line, 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 lookup. Each of the previous examples is to find and print out the qualifying rows by "-v" parameter can print out the contents do not meet the conditions of the line.

Find the line that contains the file name is not included in the test of the test, this time, the command is:

grep -v 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.