Demystifying GREP

Antonin J. (they/them) - Apr 9 '20 - - Dev Community

๐Ÿ“ƒ Cheatsheet

Search for a keyword in a file:

grep keyword file.txt
Enter fullscreen mode Exit fullscreen mode

Search using a regular expression:

grep -E 'regex' file.txt
Enter fullscreen mode Exit fullscreen mode

Search for a keyword in a directory:

grep -r keyword ./directory/path
Enter fullscreen mode Exit fullscreen mode

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

grep -Enr 'regex' ./directory/path
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”Ž 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 and tar 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"

๐Ÿ“„ 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.

grep searchkeyword file.txt
Enter fullscreen mode Exit fullscreen mode

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:

grep '^2020-0[0-4]' file.txt
Enter fullscreen mode Exit fullscreen mode

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:

grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' logfile.txt
Enter fullscreen mode Exit fullscreen mode

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.

grep -r searchkeyword directoryName
Enter fullscreen mode Exit fullscreen mode

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:

grep -flags searchkeyword directoryOrFilename
grep -ir antonin ./documents
Enter fullscreen mode Exit fullscreen mode

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)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player