Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux grep Command

Linux grep Command

Linux Command Reference Linux Command Reference

The Linux grep (global regular expression) command is used to search files for strings or regular expressions that match a given condition.

The grep command is used to find files whose contents contain a specified pattern. If a file's content matches the specified pattern, grep displays the matching line by default. If no file name is specified, or the given file name is -, grep reads data from standard input.

Syntax



grep [options] pattern [files]

或

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

Common options::

More parameter descriptions:

Linux Command Reference Linux Command Reference

Example

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 the folder dir for lines matching the regular expression "pattern", and print the file name and line number of each matching line:



grep -r -n pattern dir/

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



echo "hello world" | grep -c world

4. In the current directory, find files whose names contain "file" and whose contents contain the string "test", and print the lines containing that string. To do this, 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. Search for matching files recursively. For example, to find all files under the specified directory /etc/acpi and its subdirectories (if any) that contain the string "update", and print the contents of the lines containing it, use the following command:



grep -r update /etc/acpi 

The output is as follows:



$ grep -r update /etc/acpi #以递归的方式查找“etc/acpi”  

# files containing "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. Inverted search. The previous examples search for and print lines that match; with the "-v" option you can print the lines that do not match.

To find lines that do not contain "test" in files whose 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.