Adds guide page for bash command grep (#25769)

This commit is contained in:
Doug
2019-03-06 08:52:22 -07:00
committed by Randell Dawson
parent ffefb3cdcc
commit 973a01e218

View File

@ -1,29 +1,46 @@
--- ---
title: Bash Grep title: Bash grep
--- ---
## Bash command: grep ## Bash command: grep
`grep` stands for Global Regular Expression Print. It is used to search piped input or files for a string which can contain regex.
The `grep` command is used to find matching text in input file(s). Default output lists lines from the input file(s) which contain a match to the provided pattern. Options may be used to alter matching behavior or to provide a different output scheme.
### Usage
``` ### Usage
grep <some string> <file name>
``` ```bash
grep [options] [pattern] [file_names]
Commonly used options: ```
* `-i` - Ignores the case when performing the search. Common options:
* `-v` - Inverts the search and only selects lines that do not match the search string.
* `-i`, Ignore case when mathing the provided pattern
### Examples * `-v`, Show results which do not match the provided pattern.
#### Search for IP 127.0.0.1 in the /etc/hosts file * `-l`, Instead of outputting matching lines, output the file paths which contain matching text.
* `-r`, Search all files in the provided directories. Directory name(s) or path(s) is used in place of the file name(s) (Search defaults to the current working directory if none is provided)
* `-c`, Output a count of the matching lines.
* `-E`, Use extended regular expressions to define the pattern to be matched. The command alias `egrep` is the same as `grep -E`
### Examples:
Search a server log file for lines containing the text "ERROR":
```bash
grep ERROR server.log
```
Using a pipe to combine commands, list files and folders in the current working directory that contain the text "code":
```bash
ls | grep code
```
Search for IP 127.0.0.1 in the /etc/hosts file
```bash ```bash
grep "127.0.0.1" /etc/hosts grep "127.0.0.1" /etc/hosts
``` ```
#### Search for oom (out of memory) in /var/log/messages Search for oom (out of memory) in /var/log/messages
```bash ```bash
grep -i oom /var/log/messages grep -i oom /var/log/messages
``` ```
### More Information ### More Information
* run `man grep` for a full list of available flags/options to use. * [Bash Guide for Beginners, Examples using grep](https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html)
* [Wikipedia](https://en.wikipedia.org/wiki/Grep) * [Wikipedia](https://en.wikipedia.org/wiki/Grep)