grep

Powerful text search tool

Supplementary instructions

grep (global search regular expression(RE) and print out the line, comprehensive search for regular expressions and print out the line) is a powerful text search tool that can use regular expressions to search text and print out the line. Matching lines are printed. Specific characters used for filtering/searching. Regular expressions can be used with a variety of commands and are very flexible in use.

Options

-a --text # Don't ignore binary data.
-A <Number of displayed lines> --after-context=<Number of displayed lines> # In addition to displaying the line that conforms to the template style, display the content after this line.
-b --byte-offset # Beyond the line that matches the template style, display the content before that line.
-B<Number of displayed lines> --before-context=<Number of displayed lines> # In addition to displaying the line that matches the style, the content before that line is also displayed.
-c --count # Count the number of columns that match the template style.
-C<Number of display lines> --context=<Number of display lines> or -<Number of display lines> # In addition to displaying the column that conforms to the template style, display the content before and after that column.
-d<Perform action> --directories=<action> # This parameter must be used when specifying a directory rather than a file to be searched, otherwise the grep command will report information and stop the action.
-e<template style> --regexp=<template style> # Specify a string as the template style for finding file content.
-E --extended-regexp # Use the template style as extended ordinary notation, which means that extended regular expressions can be used.
-f<template file> --file=<rule file> # Specify a template file, whose content has one or more template styles, and let grep find the file content that meets the template conditions. The format is the template style of each column.
-F --fixed-regexp # Treat template styles as a list of fixed strings.
-G --basic-regexp # Treat the template style as a normal representation.
-h --no-filename # Do not indicate the file name to which the column belongs before displaying the column that matches the template style.
-H --with-filename # Before displaying the column that matches the template style, indicate the file name of the column.
-i --ignore-case # Ignore differences in character case.
-l --file-with-matches # List the file names whose file contents match the specified template style.
-L --files-without-match # List file names whose content does not match the specified template style.
-n --line-number # Mark the column number before displaying the column that matches the template style.
-P --perl-regexp # PATTERN is a Perl regular expression
-q --quiet or --silent # Does not display any information.
-R/-r --recursive # The effect of this parameter is the same as specifying the "-d recurse" parameter.
-s --no-messages # Do not display error messages.
-v --revert-match # Reverse search.
-V --version # Display version information.
-w --word-regexp #Only display columns that match whole words.
-x --line-regexp #Only display columns that match all columns.
-y # This parameter has the same effect as "-i".
-o # Output only the matching parts of the file.
-m <num> --max-count=<num> # Stop searching after finding num row results to limit the number of matching rows

Regular expression

^ # Start of anchor line For example: '^grep' matches all lines starting with grep.
$ # End of anchor line For example: 'grep$' matches all lines ending with grep.
. # Matches a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
* # Match zero or more previous characters. For example: '*grep' matches all lines with one or more spaces followed by grep.
.* # used together to represent any character.
[] # Match a character within a specified range, such as '[Gg]rep' matches Grep and grep.
[^] # Matches a character that is not within the specified range, such as: '[^A-Z]rep' matches lines that do not start with letters in A-Z and immediately follow rep
\(..\) # Mark matching characters, such as '\(love\)', love is marked as 1.
\< # Anchor the beginning of a word, for example: '\<grep' matches lines containing words starting with grep.
\> # Anchor the end of a word, for example 'grep\>' matches lines containing words ending with grep.
x\{m\} # Repeat character x, m times, for example: '0\{5\}' matches lines containing 5 o's.
x\{m,\} # Repeat the character x at least m times, for example: 'o\{5,\}' matches lines with at least 5 o's.
x\{m,n\} # Repeat the character x, at least m times and no more than n times. For example: 'o\{5,10\}' matches lines with 5--10 o's.
\w # Matches text and numeric characters, that is, [A-Za-z0-9], such as: 'G\w*p' matches G followed by zero or more text or numeric characters, and then p.
\W # The inverted form of \w, matches one or more non-word characters, such as period, period, etc.
\b # Word lock character, such as: '\bgrep\b' only matches grep.

Common usage of grep command

Searching for a word in a file returns a line of text containing "match_pattern":

grep match_pattern file_name
grep "match_pattern" file_name

Find in multiple files:

grep "match_pattern" file_1 file_2 file_3 ...

Output all lines except -v option:

grep -v "match_pattern" file_name

Mark match color --color=auto options:

grep "match_pattern" file_name --color=auto

Use the regular expression -E option:

grep -E "[1-9]+"
# or
egrep "[1-9]+"

Use regular expressions with the -P option:

grep -P "(\d{3}\-){2}\d{4}" file_name

Output only the matching parts of the file -o option:

echo this is a test line. | grep -o -E "[a-z]+\."
line.

echo this is a test line. | egrep -o "[a-z]+\."
line.

Count the number of lines in a file or text that contain matching strings -c option:

grep -c "text" file_name

Search the command line history for records where the git command has been entered:

history | grep git

Output the number of lines containing matching string -n option:

grep "text" -n file_name
# or
cat file_name | grep "text" -n

#Multiple files
grep "text" -n file_1 file_2

The character or byte offset at which the print style match is located:

echo gun is not unix | grep -b -o "not"
7:not
#The character offset of a string in a line is calculated from the first character of the line, and the starting value is 0. Options **-b -o** are generally always used together.

Search multiple files and find in which files matching text is found:

grep -l "text" file1 file2 file3...

grep searches files recursively

Recursive search for text in multiple levels of directories:

grep "text" . -r -n
# .Indicates the current directory.

Ignore character case in matching styles:

echo "hello world" | grep -i "HELLO"
#hello

Option -e enables multiple matching patterns:

echo this is a text line | grep -e "is" -e "line" -o
is
is
line

#You can also use the **-f** option to match multiple styles and write out the characters that need to be matched line by line in the style file.
cat patfile
aaa
bbb

echo aaa bbb ccc ddd eee | grep -f patfile -o

Include or exclude specified files from grep search results:

# Recursively search for the character "main()" only in all .php and .html files in the directory
grep "main()" . -r --include *.{php,html}

# Exclude all README files from search results
grep "main()" . -r --exclude "README"

# Exclude files in the filelist file list from search results
grep "main()" . -r --exclude-from filelist

grep with xargs using 0-valued byte suffix:

#Test file:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3

grep "aaa" file* -lZ | xargs -0 rm

# After execution, file1 and file3 will be deleted. The grep output uses the -Z option to specify the 0-value byte as the terminator file name (\0). xargs -0 reads the input and separates the file name with the 0-value byte terminator, and then Delete matching files, -Z is usually used in combination with -l.

grep silent output:

grep -q "test" filename
# No information will be output. If the command runs successfully, it returns 0, and if it fails, it returns a non-zero value. Generally used for condition testing.

Print the lines before or after matching text:

# Display the 3 lines after matching a certain result, use the -A option:
seq 10 | grep "5" -A 3
5
6
7
8

# Display the 3 lines before matching a certain result, use the -B option:
seq 10 | grep "5" -B 3
2
3
4
5

# Display the first three lines and the last three lines matching a certain result, use the -C option:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

# If there are multiple matching results, "--" will be used as the separator between each matching result:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b