Grep cheatsheet
Tags: tutorial, beginners, codenewbie, discuss
Published: 2021-04-27 08:21:55

The **grep** utility searches any given input files, selecting lines that match one or more patterns.  By default, a pattern matches an input line if the regular expression (RE) in the pattern matches the input line without its trailing new line.  An empty expression matches every line.  Each input line that matches at least one of the patterns are written to the standard output.

**grep** is used for simple patterns and basic regular expressions (BREs). Below are the frequent use cases which will be handy for developers in their day to day activity.

### Index
* [Case insensitive Search](#case-insensitive-search)
* [Get Context](#get-context)
* [Invert Match](#invert-match)
* [Show Filenames](#show-filenames)
* [Recursive Search](#recursive-search)
* [Print Matching Part](#print-matching-part)
* [Regular Expressions](#regular-expressions)
* [Search Binaries](#search-binaries)

<hr>

## Case insensitive Search

**grep -i** : Output of both commands below will be same as **-i** option does the case insensitive search.


```
grep -i hello file.txt
```

```
grep -i HELLO file.txt
```

## Get Context

To get the context of your search use the below commands.

**grep -A <num>** :  

```
grep -A 3 hello file.txt
```
This will show 3 lines of context after your match i.e *"hello"* word in this case.

**grep -B <num>** :  

```
grep -B 3 hello file.txt
```
This will show 3 lines of context before your match i.e *"hello"* word in this case.

**grep -C <num>** :  

```
grep -C 3 hello file.txt
```
This will show 3 lines of context before and after your match i.e *"hello"* word in this case.

## Invert Match

**grep -v**: Find all lines that don't match the given pattern.  

```
grep -v hello file.txt
```

## Show Filenames

**grep -l**: Show only the filenames of the files that matched

```
grep -l hello *.txt
```
Above command lists all *.txt* files which has *"hello"* word in the current directory.

## Recursive Search

**grep -r**: Search all the files in a directory

```
grep -r hello */*.txt
```
Above command lists all *.txt* files inside nested directory which has *"hello"* word in it.

## Print Matching Part

```
grep -o hello file.txt
```

Above command prints only matching part of the line (not the complete line).

## Regular Expressions

**grep -E**: This option is used for matching Regular Expressions.

```
grep -E 'hel*' file.txt
```
Above command searches for all the lines which have words starting with **hel**.


```
grep -E 'hel*|wo*' file.txt
```
Above command searches for all the lines which have words starting with **hel OR wo** 

## Search Binaries

**grep -a**: Treat binary data like its text instead of ignoring it.

Treat all files as ASCII text.  Normally grep will simply print `Binary file ... matches` if files contain binary characters.  Use of this option forces **grep** to output lines matching the specified pattern.

```
grep -a <pattern> <binary-file>
```

<hr>

## Thank you for reading
### If you like what you read and want to see more about system design, microservices and other technology-related stuff... You can follow me on [Twitter here](https://twitter.com/vishnuchi).

<a href="https://www.buymeacoffee.com/vishnuchi" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>