Linux grep Command
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][范本样式][文件或目录...]
- pattern - indicates the string or regular expression to search for.
- files - indicates the file names to search; you can search multiple files at once. If the files argument is omitted, data is read from standard input by default.
Common options::
-i: Ignore case when matching.-v: Invert the search, printing only lines that do not match.-n: Display the line number of matching lines.-r: Search files in subdirectories recursively.-l: Print only the names of matching files.-c: Print only the number of matching lines.
More parameter descriptions:
- -a or --text : Do not ignore binary data.
- -A
or --after-context= : In addition to displaying the matching line, also show the content after it. - -b or --byte-offset : Before the matching line, prefix it with the byte offset of the line's first character.
- -B
or --before-context= : In addition to displaying the matching line, also show the content before it. - -c or --count : Count the number of matching lines.
- -C
or --context= : In addition to displaying the matching line, also show the content before and after it.or - - -d
or --directories= : When the target is a directory rather than a file, this option must be used; otherwise grep will report a message and stop. - -e
or --regexp= : Specify a string as the pattern for searching file contents. - -E or --extended-regexp : Treat the pattern as an extended regular expression.
- -f
or --file= : Specify a rule file containing one or more patterns, so grep searches for file contents matching the rules, with one pattern per line. - -F or --fixed-regexp : Treat the pattern as a list of fixed strings.
- -G or --basic-regexp : Treat the pattern as a basic (ordinary) regular expression.
- -h or --no-filename : Do not prefix the matching line with the name of the file it belongs to.
- -H or --with-filename : Prefix the matching line with the name of the file it belongs to.
- -i or --ignore-case : Ignore differences in character case.
- -l or --file-with-matches : List the names of files whose contents match the specified pattern.
- -L or --files-without-match : List the names of files whose contents do not match the specified pattern.
- -n or --line-number : Prefix the matching line with its line number.
- -o or --only-matching : Display only the part that matches PATTERN.
- -q or --quiet or --silent : Do not display any output.
- -r or --recursive : This option has the same effect as specifying "-d recurse".
- -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 : Display only lines that match whole words.
- -x --line-regexp : Display only lines where the entire line matches.
- **-y **: This option has the same effect as specifying "-i".
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.
Linux Command Reference