Linux grep tutorial
Additional Notes #
grep (global search regular expression(RE) and print out the line) is a powerful text search tool that searches text using regular expressions and prints out the matching lines. Used to filter/search for specific characters. Can be used with regular expressions can be used with a variety of commands, very flexible in use.
Options #
-a --text # Don't ignore binary data.
-A <show lines> --after-context=<show lines> # Show what comes after the line in addition to the line that matches the paradigm pattern.
-b --byte-offset # Display the content before the line in addition to and before the line that matches the paradigm pattern.
-B<display-line count> --before-context=<display-line count> # Display the content before the line in addition to the line that matches the style.
-c --count # Count the number of columns that match the paradigm style.
-C<display row count> --context=<display row count> or --<display row count> # Show the content before and after the column in addition to the column that matches the paradigm style.
-d<conduct action> --directories=<action> # This argument must be used when specifying that the directory to look for is a directory and not a file, otherwise the grep command will return information and stop the action.
-e<paradigm style> --regexp=<paradigm style> # Specify a string as the paradigm style for finding the contents of a file.
-E --extended-regexp # Use the paradigm style as an extended normal representation, meaning that the use can use extended regular expressions.
-f<paradigm file> --file=<rules file> # Specify a paradigm file whose contents have one or more paradigm styles, allowing grep to find the contents of files that match the paradigm conditions, in the format of a paradigm style for each column.
-F --fixed-regexp # Treat the paradigm styles as a list of fixed strings.
-G --basic-regexp # Use the model style as a normal representation.
-h --no-filename # Do not indicate the name of the file to which the column belongs until the column matching the model style is displayed.
-H --with-filename # The file name of the column is marked before the column that matches the model style is displayed.
-i --ignore-case # Ignore character case differences.
-l --file-with-matches # List the names of files whose contents match the specified paradigm style.
-L --files-without-match # List the names of files whose contents do not match the specified paradigm pattern.
-n --line-number # Mark the column number before displaying the column that matches the paradigm pattern.
-P --perl-regexp # PATTERN is a Perl regular expression
-q --quiet or --silent # Do not show any information.
-R/-r --recursive # This argument has the same effect as specifying the "-d recurse" argument.
-s --no-messages # Do not show error messages.
-v --revert-match # Reverse the lookup.
-V --version # Show version information.
-w --word-regexp # Show only columns with full-word matches.
-x --line-regexp # Show only columns that match all columns.
-y # This parameter has the same effect as "-i".
-o # Output only the parts of the file that match.
-m <num> --max-count=<num> # Stop the search after finding num lines, used to limit the number of lines matched
Rule expressions #
^ # Start of anchored line e.g. '^grep' matches all lines starting with grep.
$ # Anchor the end of a line e.g. 'grep$' matches all lines ending in grep.
. # Match a character that is not a newline e.g. 'gr.p' matches gr followed by an arbitrary character, followed by p.
* # Match zero or more previous characters e.g. '*grep' matches all lines with one or more spaces immediately following grep.
. * # Used together to represent any character.
[] # Match a character in the specified range, e.g. '[Gg]rep' matches grep and grep.
[^] # Match a character that is not in the specified range, e.g. '[^A-FH-Z]rep' matches a line that does not contain A-R and T-Z starting with a letter that immediately follows rep.
\(. \) # Mark matching characters, e.g. '\(love\)', where love is marked as 1.
\< # Anchor the start of a word, e.g.:'\<grep' matches lines containing words starting with grep.
\> # Anchor the end of a word, e.g. 'grep\>' matches lines containing words ending in grep.
x\{m\} # Repeat the character x, m times, e.g., '0\{5\}' matches lines containing 5 o's.
x\{m,\} # Repeat the character x, at least m times, e.g., 'o\{5,\}' matches lines with at least 5 o's.
x\{m,n\} # Repeat the character x, at least m times, no more than n times, e.g., 'o\{5,10\}' matches lines with 5 - 10 o's.
\w # Match text and numeric characters, that is, [A-Za-z0-9], e.g., 'G\w*p' matches zero or more text or numeric characters followed by G, then p.
\W # The inverted form of \w, matching one or more non-word characters, such as a dotted period, etc.
\b # The single word lock character, e.g.: '\bgrep\b' matches only grep.
grep command common usage #
To search for a word in a file, the command returns a text 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 -v Option.
grep -v "match_pattern" file_name
Mark match colors --color=auto Options.
grep "match_pattern" file_name --color=auto
Use regular expressions -E Options.
grep -E "[1-9]+"
# or
egrep "[1-9]+"
Using the regular expression -P option.
grep -P "(\d{3}\-){2}\d{4}" file_name
Output only the parts of the file that match -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 matching strings **-c** Options.
```shell
grep -c "text" file_name
Output the number of lines containing the matching string -n Options.
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 style match is located.
echo gun is not unix | grep -b -o "not"
7:not
The character cheapness of a string in a # line is calculated from the first character of the line, starting at 0. The options **-b -o** are generally always used in conjunction.
Search multiple files and find which files the matching text is in.
grep -l "text" file1 file2 file3...
grep recursive search for files #
Recursively search for text in a multi-level directory: ```shell
grep "text" . -r -n
# . Indicates the current directory.
Ignore the case of characters in the match style.
echo "hello world" | grep -i "HELLO"
# hello
Option -e brakes multiple match styles: ```shell echo
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 by writing the matching characters line by line in the style file.
cat patfile
aaa
bbb
echo aaa bbb ccc ddd eee | grep -f patfile -o
Include or exclude the specified file in the grep search results.
# Search recursively for the character "main()" in all .php and .html files in the directory only
grep "main()" . -r --include *. {php,html}
# Exclude all README files from the search results
grep "main()" . -r --exclude "README"
# Exclude files from the filelist file list in the search results
grep "main()" . -r --exclude-from filelist
Use grep with a 0-value byte suffix with xargs.
# Test files.
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3
grep "aaa" file* -lZ | xargs -0 rm
# Execution deletes file1 and file3. grep output uses the -Z option to specify a 0-value byte as the terminator filename (\0). xargs -0 reads the input and separates the filenames with a 0-value byte terminator, then deletes the matching file. -Z is usually used in combination with -l.
grep Silent output.
grep -q "test" filename
# Does not output any information, returns 0 if the command runs successfully, or a non-zero value if it fails. Generally used for conditional testing.
Print out the line before or after the matching text.
# Show the 3 lines after matching a certain result, using the -A option.
seq 10 | grep "5" -A 3
5
6
7
8
# Show the 3 lines before a result is matched, using the -B option.
seq 10 | grep "5" -B 3
2
3
4
5
# Show the first three lines and the last three lines of a result, using the -C option.
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8
# If there are multiple matches, "--" will be used as a separator between the matches.
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b