grep
Powerful Text Search Tool
Supplementary Note
grep (global search regular expression (RE) and print out the line, a comprehensive search for regular expressions and print out the line) is a powerful text search tool that can use regular expressions to search for text and print out matching lines come out. Specific characters for filtering/searching. Regular expressions can be used in conjunction with various commands, which is 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, and display the content after the line. -b --byte-offset # Outside the line that matches the template style, and display the content before the line. -B<number of displayed lines> --before-context=<number of displayed lines> # In addition to displaying the line that meets the style, and display the content before the line. -c --count # Count the number of columns that match the template style. -C<number of displayed rows> --context=<number of displayed rows> or -<number of displayed rows> # In addition to displaying the column that conforms to the template style, and display the content before and after the column. -d<action> --directories=<action> # This parameter must be used when it is specified that the directory to be searched is not a file, otherwise the grep command will report information and stop the action. -e<template style> --regexp=<template style> # Specify a string as a template style for finding file content. -E --extended-regexp # Use the template style as an extended common expression, which means that the extended regular expression can be used. -f<template file> --file=<rule file> # Specify the template file, its content has one or more template styles, let grep find the file content that meets the template conditions, and the format is the template style of each column. -F --fixed-regexp # Treat template styles as a list of fixed strings. -G -h --no-filename # Before displaying the column that conforms to the template style, do not indicate the file name to which the column belongs. -H --with-filename # Before displaying the column that conforms to the template style, mark the file name of the column. -i --ignore-case # Ignore the difference in character case. -l --file-with-matches # List the file names whose content matches the specified template style. -L --files-without-match # List the file names whose content does not match the specified template style. -n --line-number # Before displaying the line that matches the template style, mark the number of the line. -P --perl-regexp # PATTERN is a Perl regular expression -q --quiet or --silent # Do 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 the match. -V --version # Display version information. -w --word-regexp # Only display columns that match all words. -x --line-regexp # Only display columns that match all columns. -y # This parameter has the same effect as "-i". -o # Only output the matched part of the file. -m <num> --max-count=<num> # Stop searching after finding num rows of results, used to limit the number of matching rows
regular expression
^ # The beginning of the anchor line such as: '^grep' matches all lines beginning with grep. $ # Anchor the end of the line eg: 'grep$' matches all lines ending with grep. . # Match a non-newline character such as: 'gr.p' matches gr followed by any character, then p. * # Match zero or more previous characters such as: '*grep' matches all one or more spaces followed by grep lines. .* # Used together to represent any character. [] # Match a character within a specified range, such as '[Gg]rep' matches Grep and grep. [^] # Match a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a line beginning with a letter that does not contain AR and TZ, followed by rep. \(..\) # Mark matching characters, such as '\(love\)', love is marked as 1. \< # Anchor the beginning of a word, such as: '\<grep' matches lines containing words beginning with grep. \> # Anchor the end of a word, such as 'grep\>' matches lines containing words ending with grep. x\{m\} # Repeat character x, m times, such as: '0\{5\}' matches a line containing 5 o. x\{m,\} # Repeat the character x, at least m times, such as: 'o\{5,\}' matches at least 5 lines of o. x\{m,n\} # Repeat character x, at least m times, no more than n times, such as: 'o\{5, 10\}' matches lines with 5--10 o's. \w # Matches alphanumeric characters, that is, [A-Za-z0-9], such as: 'G\w*p' matches G followed by zero or more alphanumeric characters, then p. \W # The inverted form of \w matches one or more non-word characters, such as dots, periods, etc. \b # word lock character, such as: '\bgrep\b' only matches grep.
Common usage of grep command
To search for a word in a file, the command returns a line 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 the -v option:
grep -v "match_pattern" file_name
Mark matching color --color=auto option:
grep "match_pattern" file_name --color=auto
Use the regex -E option:
grep -E "[1-9]+" # or egrep "[1-9]+"
Use the regular expression -P option:
grep -P "(\d{3}\-){2}\d{4}" file_name
Only output the part of the file that matches the -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 containing a matching string -c option:
grep -c "text" file_name
Output the number of lines containing the matched string -n option:
grep "text" -n file_name # or cat file_name | grep "text" -n #multiple files grep "text" -n file_1 file_2
Print the character or byte offset at which the pattern matches:
echo gun is not unix | grep -b -o "not" 7:not #The character value of the string in a line is calculated from the first character of the line, and the initial value is 0. The options **-b -o** are generally always used together.
Search multiple files and find which files the matching text is in:
grep -l "text" file1 file2 file3...
grep searches files recursively
Do a recursive search for text in a multi-level directory:
grep "text" . -r -n # . Indicates the current directory.
Character case is ignored in matching patterns:
echo "hello world" | grep -i "HELLO" # hello
Option -e brakes multiple matching patterns:
echo this is a text line | grep -e "is" -e "line" -o is line #You can also use the **-f** option to match multiple styles, and write out the lines that need to be matched line by line in the style file character. cat patfile aaa bbb echo aaa bbb ccc ddd eee | grep -f patfile -o
Include or exclude specified files in grep search results:
# Only recursively search for the character "main()" 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 and xargs with 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, grep output with -Z option to specify a zero-valued byte as a terminator filename (\0), xargs -0 reads the input and separates the filenames with a zero-valued byte terminator, then deletes matching files, -Z is usually used in combination with -l.
grep silently outputs:
grep -q "test" filename # Will not output any information. If the command runs successfully, it returns 0, and if it fails, it returns a non-zero value. Typically used for conditional testing.
Print the line before or after the 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