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
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Search for an exact string:
|
2015-10-22 15:31:52 +08:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep {{search_string}} {{file_path}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-04-22 12:24:24 +01:00
|
|
|
- Search in case-insensitive mode:
|
2016-01-28 00:12:10 +02:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -i {{search_string}} {{path/to/file}}`
|
2016-01-28 00:12:10 +02:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
- Search recursively (ignoring non-text files) in current directory for an exact string:
|
2014-01-28 13:41:32 +01:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -rI {{search_string}} .`
|
2014-01-28 13:41:32 +01:00
|
|
|
|
2016-07-04 17:40:04 -03:00
|
|
|
- Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`):
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-07-04 17:40:04 -03:00
|
|
|
`grep -E {{^regex$}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-04-22 12:24:24 +01:00
|
|
|
- Print 3 lines of context around each match:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -C 3 {{search_string}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Print the count of matches instead of the matching text:
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -c {{search_string}} {{path/to/file}}`
|
2013-12-08 19:56:16 +11:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Print line number for each match:
|
2016-01-05 00:30:21 -05:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -n {{search_string}} {{path/to/file}}`
|
2016-01-05 00:30:21 -05:00
|
|
|
|
2016-06-21 15:11:27 +08:00
|
|
|
- Print file names with matches:
|
|
|
|
|
2016-06-24 10:39:17 +01:00
|
|
|
`grep -l {{search_string}} {{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
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`cat {{path/to/file}} | grep {{search_string}}`
|
2014-07-27 10:23:56 +02:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Invert match for excluding specific strings:
|
2014-07-27 10:23:56 +02:00
|
|
|
|
2016-06-23 18:50:38 +01:00
|
|
|
`grep -v {{search_string}}`
|