1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-08-05 10:15:41 +02:00
tldr/pages/common/grep.md
Managor ba6daef39d
*: standardize regex usage (#17328)
Co-authored-by: Dylan <145150333+dmmqz@users.noreply.github.com>
2025-07-25 08:50:06 +03:00

1.3 KiB

grep

Find patterns in files using regexes. More information: https://www.gnu.org/software/grep/manual/grep.html.

  • Search for a pattern within a file:

grep "{{search_pattern}}" {{path/to/file}}

  • Search for an exact string (disables regexes):

grep {{[-F|--fixed-strings]}} "{{exact_string}}" {{path/to/file}}

  • Search for a pattern in all files recursively in a directory, showing line numbers of matches, ignoring binary files:

grep {{[-rnI|--recursive --line-number --binary-files=without-match]}} "{{search_pattern}}" {{path/to/directory}}

  • Use extended regexes (supports ?, +, {}, (), and |), in case-insensitive mode:

grep {{[-Ei|--extended-regexp --ignore-case]}} "{{search_pattern}}" {{path/to/file}}

  • 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}}

  • Search for lines matching a pattern, printing only the matched text:

grep {{[-o|--only-matching]}} "{{search_pattern}}" {{path/to/file}}

  • Search stdin for lines that do not match a pattern:

cat {{path/to/file}} | grep {{[-v|--invert-match]}} "{{search_pattern}}"