# grep - perldoc - phpman

> **TLDR:** Find patterns in files using `regex`es.
>
- Search for a pattern within files:
  `grep "{{search_pattern}}" {{path/to/file1 path/to/file2 ...}}`
- Search for an exact string (disables `regex`es):
  `grep {{-F|--fixed-strings}} "{{exact_string}}" {{path/to/file}}`
- Search for a pattern in all files recursively in a directory, ignoring binary files:
  `grep {{-rI|--recursive --binary-files=without-match}} "{{search_pattern}}" {{path/to/directory}}`
- Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
  `grep {{-context|--before-context|--after-context}} 3 "{{search_pattern}}" {{path/to/file}}`
- Print file name and line number for each match with color output:
  `grep {{-Hn|--with-filename --line-number}} --color=always "{{search_pattern}}" {{path/to/file}}`
- Print only the matched text:
  `grep {{-o|--only-matching}} "{{search_pattern}}" {{path/to/file}}`
- Read data from `stdin` and do not print lines that match a pattern:
  `cat {{path/to/file}} | grep {{-v|--invert-match}} "{{search_pattern}}"`
- Use extended `regex`es (supports `?`, `+`, `{}`, `()`, and `|`), in case-insensitive mode:
  `grep {{-Ei|--extended-regexp --ignore-case}} "{{search_pattern}}" {{path/to/file}}`

*Source: tldr-pages*

---

    grep BLOCK LIST
    grep EXPR,LIST
            This is similar in spirit to, but not the same as, [grep(1)](https://www.chedong.com/phpMan.php/man/grep/1/markdown) and
            its relatives. In particular, it is not limited to using regular
            expressions.

            Evaluates the BLOCK or EXPR for each element of LIST (locally
            setting $_ to each element) and returns the list value
            consisting of those elements for which the expression evaluated
            to true. In scalar context, returns the number of times the
            expression was true.

                my @foo = grep(!/^#/, @bar);    # weed out comments

            or equivalently,

                my @foo = grep {!/^#/} @bar;    # weed out comments

            Note that $_ is an alias to the list value, so it can be used to
            modify the elements of the LIST. While this is useful and
            supported, it can cause bizarre results if the elements of LIST
            are not variables. Similarly, grep returns aliases into the
            original list, much as a for loop's index variable aliases the
            list elements. That is, modifying an element of a list returned
            by grep (for example, in a "foreach", "map" or another "grep")
            actually modifies the element in the original list. This is
            usually something to be avoided when writing clear code.

            See also "map" for a list composed of the results of the BLOCK
            or EXPR.

