Just this morning at work I had to search a file for occurences of a certain keyword (to keep this interesting let’s say I had to search for a password named NUCLEAR_CODES_PASSWORD inside a file). It would be easy to just import that file into Visual Studio Code, click Ctrl + F
, and search the file for occurences.
Well, it would be even easier (and more hackerish) to navigate to the folder inside my terminal, type in a command, and get the result of my search almost instantly, right? Well, thankfully the terminal has a command that is pretty useful with use cases like these. It is called grep
.
grep
is a powerful command line utility used to find text patterns within files. It stands for global regular expression print. It is used like this:
grep [options] pattern [files]
Pattern is the text we’re searching for in our files. When the command encounters any patterns in the file (or files) it runs upon, it will return/print the lines that contain those patterns. We can match simple patterns, but we can also search for really complex ones. Grep can also filter out regular expressions (a sequence of characters that define a pattern).
Files are self-explanatory, they are the elements the pattern search will be run upon.
Options are additional flags that can tweak the search.
The examples
Let’s jump right into the examples. I have a file called passwords.txt
on my machine. If I want to check the contents of the file, I can write the following command:
$ cat passwords.txt FACEBOOK_PASSWORD=q.Gcyt;Z`3S8$'@H INSTAGRAM_PASSWORD=GhgpgYy4P2PaZY2U NUCLEAR_CODES_PASSWORD=0000 BICYCLE_LOCK_PASSWORD=123 PHONE_PASSWORD=8UeNwDFskR9r9LxZ
OK, if I am searching for NUCLEAR_CODES_PASSWORD it is obviously not that hard to notice it. We only have five lines in this document. But, what if the file was really long – 100, 1000, or even more lines in the document? Then navigating the file, and noticing changes by the naked eye wouldn’t be that easy.
Below you can see some of the most common uses of grep inside the terminal.
Searching inside a file
To print out lines from the file that contain a specific pattern of characters, let us say our NUCLEAR_CODES_PASSWORD we could do something like this:
$ grep NUCLEAR_CODES_PASSWORD passwords.txt NUCLEAR_CODES_PASSWORD=0000
Searching inside multiple files
We can also search multiple files, by inserting the filenames you want to search. Let’s say we have an additional file inside here, called passwords2.txt
. We could search both of those files by doing this:
$ grep NUCLEAR_CODES_PASSWORD passwords.txt passwords.txt:NUCLEAR_CODES_PASSWORD=0000 passwords2.txt:NUCLEAR_CODES_PASSWORD=a_more_complex_code_1234_
Case Insensitive search
If we add the -i
option to the search we can ignore the string case sensitivity. ‘nuclear’ would return anything including ‘NUCLEAR’, ‘nUcLeAR’ or ‘nuclear’.
$ grep -i nuclear passwords.txt NUCLEAR_CODES_PASSWORD=0000
Displaying the number of matches
We can also search for the total number of matches by using the -c
option. Here we searched the whole file for the ‘password’ pattern (we also combined options by adding i
, so we ignored the case). We get 5 because ‘password’ appears five times in our document.
$ grep -ci password passwords.txt 5
Showing Line Numbers
By using -n
we can show the line for every occurrence.
$ grep -ni password passwords.txt 1:FACEBOOK_PASSWORD=q.Gcyt;Z`3S8$'@H 2:INSTAGRAM_PASSWORD=GhgpgYy4P2PaZY2U 3:NUCLEAR_CODES_PASSWORD=0000 4:BICYCLE_LOCK_PASSWORD=123 5:PHONE_PASSWORD=8UeNwDFskR9r9LxZ
Caret (^) metacharacter
Caret metacharacter matches patterns at the beginning of the line. Only lines that start with ‘instagram’ (again -i
ignores case) will be matched.
$ grep -i "^instagram" passwords.txt INSTAGRAM_PASSWORD=GhgpgYy4P2PaZY2U
Dollar sign ($) metacharacter
Dollar sign metacharacter will only match patterns at the end of the line, and return those lines. We only have one line having ‘0000’ as the ending sequence, so we return that line. (again we ignored the case, but here that isn’t so important)
$ grep -i "0000$" passwords.txt NUCLEAR_CODES_PASSWORD=0000
Conclusion
We only scratched the surface here, grep
is really complex and has lots of functionality.
If you want to read more about grep
you can check out the official documentation.
Additionally, you can read more blog posts here.