English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux grep command

Linux Command大全

The Linux grep command is used to search for strings that match the specified pattern in files.

The Linux grep command is used to search for strings that match the specified pattern in files. If a file's content matches the specified pattern, the grep command will by default display the column containing the pattern style. If no file name is specified, or the given file name is -If no file name is specified, or the given file name is

Syntax

grep [-abcEFGhHilLnqrsvVwxy][-A<number of lines to display>][-B<number of columns to display>][-C<number of columns to display>][-d<action>][-e<pattern style>][-f<pattern file>][--help][pattern style][file or directory...]

Parameters:

  • -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 column that matches the pattern style, also display the content after the line.

  • -b Or --byte-offset Indicate the position of the first character of the line before displaying the line that matches the style.

  • -B<number of lines to display> Or  --before-context=<number of lines to display> In addition to displaying the line that matches the style, also display the content before the line.

  • -c Or  --count Calculate the number of columns that match the style.

  • -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 style, also display the content before and after the line.

  • -d <action> Or    --directories=<action> This parameter must be used when specifying a directory instead of a file, otherwise the grep command will return an error message and stop the operation.

  • -e<pattern style> Or --regexp=<pattern style> Specify a string as the style for searching file content.

  • -Or  --extended-regexp Use the style as an extended regular expression.

  • -f<rule file> Or --file=<rule file> Specify a rule file containing one or more rule styles, so that grep can search for file content that matches the rule conditions, with each line containing a rule style.

  • -Or --fixed-regexp Use the style as a list of fixed strings.

  • -Or --basic-regexp Use the style as a regular expression.

  • -Or --no-filename Do not indicate the file name of the line before displaying the line that matches the style.

  • -H  或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。

  • -i  或  --ignore-case : 忽略字符大小写的差别。

  • -l   或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。

  • -L 或  --files-without-match : 列出文件内容不符合指定的样式的文件名称。

  • -n  或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。

  • -o 或 --only-matching : 只显示匹配PATTERN 部分。

  • -q  或 --quiet或--silent : 不显示任何信息。

  • -r  或 --recursive : 此参数的效果和指定"-d recurse"参数相同。

  • -s   或 --no-messages : 不显示错误信息。

  • -v   或 --invert-match : 显示不包含匹配文本的所有行。

  • -V  或 --version : 显示版本信息。

  • -w 或  --word-regexp : 只显示全字符合的列。

  • -x    --line-regexp : 只显示全列符合的列。

  • -y : 此参数的效果和指定"-i"参数相同。

Linux Command大全

在线示例

1、在当前目录中,查找后缀有 file 字样的文件中包含 test 字符串的文件,并打印出该字符串的行。此时,可以使用如下命令:

grep test *file

Results as shown below:

$ 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、以递归的方式查找符合条件的文件。例如,查找指定目录/etc/acpi 及其子目录(如果存在子目录的话)下所有文件中包含字符串"update"的文件,并打印出该字符串所在行的内容,使用的命令为:

grep -r update /etc/acpi

输出结果如下:

$ 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

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

To find files whose names contain 'test' but do not contain 'test' in the lines, the following command is used:

grep -v test *test*

Results as shown below:

$ grep-v test* #Find files whose names contain 'test' but do not contain 'test' in the lines  
testfile1:helLinux!  
testfile1:Lin is a free Unix-type operating system.  
testfile1:Lin  
testfile_1:HELLO LINUX!  
testfile_1:LINUX IS A FREE UNIX-TYPE OPERATING SYSTEM.  
testfile_1:THIS IS A LINUX TESTFILE!  
testfile_2:HELLO LINUX!  
testfile_2:Linux is a free unix-Type operating system.