Grep

grep(1) is a searching tool.


Installation

grep(1) will be pre-installed on any Linux or BSD operating system, as a POSIX utility.


Usage

grep(1) reads lines from files and prints any that match a pattern. The name is an homage to a functionally-equivalent ed(1) script: g/REGEX/p.

Try:

grep --regexp='regex' somefile
#or: grep -e 'regex' somefile

To recursively read lines from all files under a directory, try:

grep --regexp='regex' --recursive somedir
#or: grep -e 'regex' -r somedir

Files can be named as either positional arguments or as named arguments on --file or -f flags.

If no file is named as a target, or if - is named as the target, then grep(1) operates on STDIN.

Note that if no file is named as a target, or if - is named as the target, and the --recursive flag was given, then grep(1) will instead read all files in the working directory.

Fixed Patterns

In some cases, regular expression patterns are unnecessary. To instead search for a fixed pattern, try:

grep --fixed-strings='literal' somefile
#or: grep -F 'literal' somefile

Perl Patterns

On the other hand, sometimes GNU regular expressions are insufficient. To instead search with PCRE, try:

grep --perl-regexp='regex' somefile
#or: grep -P 'regex' somefile

Multiple Patterns

To search with multiple patterns, i.e. a logical OR, simply specify multiple pattern flags. Try:

grep --regexp='regex' --fixed-strings='literal' --perl-regexp='regex' somefile

To search for the intersections of multiple patterns, i.e. a logical AND, try piping distinct commands.

grep --regexp='regex' somefile | grep --fixed-strings='literal'

Inversion

to invert the behavior of grep(1), i.e. print lines that do not match, try:

grep --regexp='regex' --invert-match somefile
#or: grep --regexp='regex' -v somefile

Extraction

To print only the part of a line that matches the pattern, try:

grep --regexp='regex' --only-matching somefile
#or: grep --regexp='regex' -o somefile

Note that for lines that match repeatedly, each match is printed on a new line.

File Identification

grep(1) also supports functionality for identifying files that match.

To print the names of files that match, try:

grep --regexp='regex' --files-with-matches somefile
#or: grep --regexp='regex' -l somefile

The inverted behavior, i.e. printing the names of files that do not match, is triggered with --files-without-match (-L).

To print the names of files and the total count of matches in that file, try:

grep --regexp='regex' --count somefile
#or: grep --regexp='regex' -c somefile

The inverted behavior, i.e. printing the total count of non-matches, is triggered with the same --invert-match (-v) as before.

Note that files with 0 matches are also printed in this mode.

Programming Tips

grep(1) can be used programmatically to test for the existence of a match. The return code is set to success (0) if a match is found. To suppress output though, use the --quiet (-q) flag.

To suppress warnings about non-existent and unreadable files, use the --no-messages (-s) flag.


See also

grep(1)


CategoryRicottone

Grep (last edited 2024-09-16 14:15:46 by DominicRicottone)