Lesson 10: Intro to Grep
Channel: BASH The Command Line
Date: 2021-09-13

Today's lesson is all about grep.

If you don't already have the files we downloaded in an earlier video, go get the zip file at the link that says "Download the files used in this book" at https://automatetheboringstuff.com/ and unzip the file into your home directory so you can practice along side the video. (Not my book, but Al Sweigart's python books and courses are actually great. I recommend them! And, no, he's not paying me to say that. :) )

grep [options] [pattern] [file(s) to search]

options we cover in this video:
-i          case insensitive (don't care if it's caps or lower case)
-w        word. Use the pattern to match the whole word, don't match inside another word.
-n         show line numbers
-b         show location in the file (lit. the 0-based byte offset of the location of the output)
-o        only show the thing that matches the requested pattern (not the rest of the line)
-B n     show n lines before the line with the pattern match
-A n     show n lines after the line with the pattern match
-C n     show n lines before and after the line with the pattern match
-E        use a regular expression (not just a simple string) for pattern match
-l         show only the names of the files with matches, not the line with the match

Another useful option I ran out of time for is
-c      count. Show the number of times this pattern has been matched. Used by itself, it just tells you how many matches there are total. Try using this with -l to see the number of matches per file. Can you find other ways to use the count option?