2013-12-08 19:56:16 +11:00
|
|
|
# grep
|
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
> Matches patterns in input text.
|
|
|
|
> Supports simple patterns and regular expressions.
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
- Search for a pattern within a file:
|
2015-10-22 15:31:52 +08:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep {{search_pattern}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
- Search for an exact string:
|
2016-01-28 00:12:10 +02:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep -F {{exact_string}} {{path/to/file}}`
|
2016-01-28 00:12:10 +02:00
|
|
|
|
2020-10-11 02:46:14 +07:00
|
|
|
- Search for a pattern [R]ecursively in the current directory, showing matching line [n]umbers, [I]gnoring non-text files:
|
2014-01-28 13:41:32 +01:00
|
|
|
|
2020-10-11 02:46:14 +07:00
|
|
|
`grep -RIn {{search_pattern}} .`
|
2014-01-28 13:41:32 +01:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
- Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`), in case-insensitive mode:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep -Ei {{search_pattern}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-08-29 05:13:14 +05:30
|
|
|
- Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep -{{C|B|A}} 3 {{search_pattern}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2017-12-07 08:53:40 +05:30
|
|
|
- Print file name with the corresponding line number for each match:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep -Hn {{search_pattern}} {{path/to/file}}`
|
2016-06-21 15:11:27 +08:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Use the standard input instead of a file:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`cat {{path/to/file}} | grep {{search_pattern}}`
|
2014-07-27 10:23:56 +02:00
|
|
|
|
2021-02-02 16:44:00 +04:00
|
|
|
- In[v]ert match for excluding specific strings:
|
2014-07-27 10:23:56 +02:00
|
|
|
|
2020-06-09 10:34:03 +02:00
|
|
|
`grep -v {{search_pattern}}`
|