Demystifying GREP
Tags: linux, shell, productivity
Published: 2020-04-09 23:16:43

---
title: Demystifying GREP
published: true
description: 
tags: linux, shell, productivity
cover_image: https://antjanus.com/wp-content/uploads/2020/04/demystifying-grep.png
canonical_url: https://antjanus.com/digital-garden/demystifying-grep
---

## 📃 Cheatsheet

Search for a keyword in a file:

```shell
grep keyword file.txt
```

Search using a regular expression:

```shell
grep -E 'regex' file.txt
```

Search for a keyword in a directory:

```shell
grep -r keyword ./directory/path
```

Combine multiple flags/options to search through a directory, display line numbers, and use regex:

```shell
grep -Enr 'regex' ./directory/path
```

## 🔎 What is grep?
Grep is a CLI utility for plain text searches with regex (regular expressions). It's like a Ctrl/Command + F but through a bunch of files and in the command line.

It's another one of those ancient utilities that have proliferated through all Linux and Unixy distributions. Similar to [Rsync](https://dev.to/antjanus/demystifying-rsync-3jac) and [tar](https://dev.to/antjanus/demystifying-tar-351l) that I've talked about in the past.

Fortunately, unlike rsync or tar, grep requires a lot less flags and knowledge of the tool to be very productive with it.

**Note:** Are you on Windows? Check out my [article on "how to 'grep' in Powershell"](https://antjanus.com/blog/web-development-tutorials/how-to-grep-in-powershell/)

## 📄 Searching for a keyword in a file

If you're in the command line and you don't necessarily want to open a file, or you're dealing with an enormous file, it can be very handy to look for just one line of text using grep. 

```shell
grep searchkeyword file.txt
```

Grep will return all the lines containing the text. Depending on your grep variant, you might get line numbers, the keyword highlighted, and other fun information.

## 🔬 Searching using regular expressions with the `-E` flag

Need to use a regular expression because you're a pro at those? Wrapping a regex in quotes will do it most of the time; however, some syntax isn't supported this way so you have to use the `-E` flag. For example:

```shell
grep '^2020-0[0-4]' file.txt
```
This will work just fine, the `[]` syntax is supported in basic regex mode; however, specifying occurrences with `{}` won't work so I recommend defaulting to the `-E` flag for any regex so you can do neat stuff like this:

```shell
grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' logfile.txt
```
This regex will fail in basic mode but the extended mode will match any `yyyy-mm-dd` date anywhere within your text. 

## 📂 Searching through multiple files/directory

Grep lets you also recursively search through many files and directories.

```shell
grep -r searchkeyword directoryName
```
Grep will return all of the matching lines *and* tell you in which files they were. 

## 🏁 More flags

Grep has a great deal of options. Their manual page is *very* long and I suggest giving it a look when you feel like grep's basic capabilities just aren't enough for you.

Here's the syntax to add or combine flags when using grep:

```shell
grep -flags searchkeyword directoryOrFilename
grep -ir antonin ./documents
```
In the example, `-ir` combines both the `i` and the `r` flag. Similarly, `-Er` would let us recursively search with extended regex.

Quick run down of some of these useful flags:

- `i` - case insensitive flag
- `n` - show line numbers in the matches
- `o` - print only the matches, and not the lines (particularly useful with regex)