2013-12-08 12:56:16 +04:00
|
|
|
# grep
|
|
|
|
|
2016-01-07 20:31:27 +03:00
|
|
|
> Matches patterns in input text.
|
|
|
|
> Supports simple patterns and regular expressions.
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-01-07 20:31:27 +03:00
|
|
|
- Search for an exact string:
|
2015-10-22 10:31:52 +03:00
|
|
|
|
2016-07-13 11:53:22 +03:00
|
|
|
`grep {{search_string}} {{path/to/file}}`
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-04-22 14:24:24 +03:00
|
|
|
- Search in case-insensitive mode:
|
2016-01-28 01:12:10 +03:00
|
|
|
|
2016-06-23 20:50:38 +03:00
|
|
|
`grep -i {{search_string}} {{path/to/file}}`
|
2016-01-28 01:12:10 +03:00
|
|
|
|
2016-06-23 20:50:38 +03:00
|
|
|
- Search recursively (ignoring non-text files) in current directory for an exact string:
|
2014-01-28 16:41:32 +04:00
|
|
|
|
2018-05-22 20:10:21 +03:00
|
|
|
`grep -RI {{search_string}} .`
|
2014-01-28 16:41:32 +04:00
|
|
|
|
2016-07-04 23:40:04 +03:00
|
|
|
- Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`):
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-07-04 23:40:04 +03:00
|
|
|
`grep -E {{^regex$}} {{path/to/file}}`
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-08-29 02:43:14 +03:00
|
|
|
- Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-08-29 02:43:14 +03:00
|
|
|
`grep -{{C|B|A}} 3 {{search_string}} {{path/to/file}}`
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2017-12-07 06:23:40 +03:00
|
|
|
- Print file name with the corresponding line number for each match:
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2017-12-07 06:23:40 +03:00
|
|
|
`grep -Hn {{search_string}} {{path/to/file}}`
|
2016-06-21 10:11:27 +03:00
|
|
|
|
2016-01-07 20:31:27 +03:00
|
|
|
- Use the standard input instead of a file:
|
2013-12-08 12:56:16 +04:00
|
|
|
|
2016-06-23 20:50:38 +03:00
|
|
|
`cat {{path/to/file}} | grep {{search_string}}`
|
2014-07-27 12:23:56 +04:00
|
|
|
|
2016-01-07 20:31:27 +03:00
|
|
|
- Invert match for excluding specific strings:
|
2014-07-27 12:23:56 +04:00
|
|
|
|
2016-06-23 20:50:38 +03:00
|
|
|
`grep -v {{search_string}}`
|