You have a problem and you want to solve it with regular expressions. Now you have two problems. This is just one of the many regular expression jokes from the internet. In this lesson, you will learn how regular expressions work. As we will be using them in the next chapters, we have prepared a file for our playground. And if you want to try the grep commands on your own, you can take it from the GitHub repository.
Let's start by opening our text file so we can see its content and then splitting up the screen. So you can see both the file and the command side by side. First of all, the simplest and probably most common regular expression is to find a single word. For this we will use the command grep Joe file dot txt. Jo is a string we are searching for and five are txt is the file where we perform the search. You can see that grep printed The line that contains our string, and the word is highlighted with another color.
This will only match the exact case of the word. So if we use lowercase G, this regex will not work anymore. To do a case insensitive search grep has n minus e option grep minus e Joe file dot txt. If we don't exactly know what characters are in our string, we can use the dot Asterix to match any number of characters. For example, to find a sentence beginning with Word and ending with day, we use the following command. The dot matches any character and the Asterix after dot matches the previous character multiple times.
So any character multiple times here, you can See that it matched the first line in the file. A very common scenario is to find empty lines in a file. For this we use grep carrot, well grep double quotes, carrot, backslash S dollar sign, double codes. File txt, where backslash S stands for space, carrot for the beginning of the line, and the dollar sign for attending. These are called anchors. We have two empty lines with no space.
If we add the space on the lines, it will match the lines containing one space grep can do a neat little trick to count the number of matches. For this we use the C parameter. To find all the lines that have only letters and space use grab open codes Carrots dollar from the beginning of the line to the end. dub, square brackets and Asterix to match a specific specific characters any number of times, and a to z A to Z lowercase for any upper and lowercase letters. If we run the command until here, we get only the first slide. If we add zero to nine and the number, we match another other two lines.
So if we add also if we add backslash s, we also match any space. We also match the empty lies and the all caps line. Here is the command. Oh my god, it's so interesting to read. I don't know how this would sound try to read it sometimes Need to search for something not in the string, in which case, this command will find all the lines that do not have only numeric characters, square brackets, and after them, the carrot sign means that to match all characters that are not inside those brackets, in our case, any non number, it matches any non number. Okay?
The square brackets are markers in our regular expression. If we want to use them in our search string, we have to escape them. So in order to find lines that have content between square brackets do execute this command. This means any line that has characters in square brackets. The same goes for the exclamation mark to find all lines that have the characters x, the exclamation marks execute this command. Now let's have a look at basic said Find and Replace said double quotes as slash Jo slash all search g double quotes file dot txt.
This will replace every occurrence of the string Joe with a string all we will go into more details about set in its own chapter. Regular expressions, just like vim are one of the things a lot of people are afraid of, because it seems complicated to learn in the beginning. Although they might seem cryptic once mastered regular expressions come to your handy companion. They are not limited to your shell, because the syntax is very similar in most programming languages, databases, editors, and any other place that includes searching for strings. We will go into more details about regular expressions in the A chapter dedicated to set