LinuxCommandLibrary

greed

Search file(s) for matching text patterns

SYNOPSIS

grep [OPTIONS] PATTERN [FILE...]

PARAMETERS

-i, --ignore-case
    Ignore case distinctions in both the PATTERN and the input files.

-v, --invert-match
    Invert the sense of matching, to select non-matching lines.

-c, --count
    Suppress normal output; instead print a count of matching lines for each input file.

-n, --line-number
    Prefix each line of output with the 1-based line number within its input file.

-r, --recursive
    Read all files under each directory, recursively. This is equivalent to -d recurse.

-l, --files-with-matches
    Suppress normal output; instead print the name of each input file from which output would normally have been printed.

-L, --files-without-matches
    Suppress normal output; instead print the name of each input file from which no output would normally have been printed.

-w, --word-regexp
    Select only those lines containing matches that form whole words.

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE). This is the default with 'egrep'.

-F, --fixed-strings
    Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. This is the default with 'fgrep'.

-A NUM, --after-context=NUM
    Print NUM lines of trailing context after matching lines.

-B NUM, --before-context=NUM
    Print NUM lines of leading context before matching lines.

-C NUM, --context=NUM
    Print NUM lines of output context (leading and trailing).

DESCRIPTION

grep (Global Regular Expression Print) is a powerful command-line utility on Linux and Unix-like systems. It's designed to search for lines that match a specified pattern within one or more input files, or from standard input.

The name 'grep' originates from the ed editor command g/re/p (global / regular expression / print), which performs a global search for a regular expression and prints all matching lines. It is an essential tool for system administrators, developers, and anyone who needs to quickly find, filter, or analyze textual data. grep is highly versatile, supporting various regular expression syntaxes and numerous options to control its search behavior, output formatting, and file handling.

CAVEATS

When searching large directories or large files, grep can consume significant system resources. Complex regular expressions can sometimes lead to unexpected performance issues or non-obvious matches. Special characters within your pattern (e.g., $, *, ., ^, [], ()) often need to be escaped with a backslash (\) or enclosed in single quotes to prevent the shell from interpreting them.

NO 'GREED' COMMAND

Please note that there is no standard Linux command officially named 'greed'. This response has been tailored assuming a likely typo for the widely used and critically important 'grep' command. If you were indeed referring to a custom or less common utility, please clarify.

REGULAR EXPRESSIONS

grep's core power comes from its use of regular expressions (regex). These are sequences of characters that define a search pattern. grep supports Basic Regular Expressions (BRE) by default, and Extended Regular Expressions (ERE) with the -E option (or by using the egrep command). Fixed strings, where the pattern is treated literally without regex interpretation, can be used with the -F option (or fgrep).

EXIT STATUS

grep communicates its success or failure through its exit status, which is crucial for scripting:

  • 0: One or more lines were selected (a match was found).
  • 1: No lines were selected (no match was found).
  • 2: An error occurred (e.g., file not found, bad option).

HISTORY

grep was originally written by Ken Thompson in 1970 for the Unix operating system. Its inception was driven by the need for a powerful text searching utility, leveraging the concept of regular expressions. It quickly became a fundamental tool in the Unix toolkit, embodying the Unix philosophy of 'do one thing and do it well'. Its design and functionality have been highly influential, inspiring countless other text processing utilities and programming language features.

SEE ALSO

sed(1), awk(1), find(1), xargs(1), cat(1), less(1), cut(1), sort(1)

Copied to clipboard