In this lesson, we'll explore regular expressions also called regex for short.
Now, in our previous commands,
we use simple search patterns looking for
some specific pieces of text like password.
But what if we need more complex search conditions?
Imagine we have some application code scattered in hundreds of files and we
need to extract all the IP addresses that are used in this app
that would require more advanced search instructions
because an IP has a form like 203.102 0.3 0.5.
But we can't just make a search pattern look for numbers with a period between
them as this would also match numbers like 5.23 which are not IP addresses.
Now, in math, we can say something like X is an integer and X is bigger than three
X is smaller than eight.
And this would mean that X is either 456 or seven.
Now, regular expressions work in a similar way.
We can specify some conditions,
tie them all together and our search pattern
only matches what perfectly fits within those conditions.
Let's start out with some super simple examples and
then we'll build up to slightly more advanced expressions.
All regular expressions are built with the help of operators like carrot,
dollar sign, period, asterisk plus sign braces, question mark,
vertical pipe brackets, parentheses, square brackets with Carrot.
Now, let's see what each of them does.
Now, in Linux configuration files can have lines that begin with a pound sign.
Now, these are called commented lines, they're inactive and they're comments.
The program looking for settings in the file will
ignore all lines that begin with a pound sign.
But comments are useful for humans as they let us see examples
of config settings in that file descriptions for what they do,
things like that without interfering with the program that reads them.
And this means that we can search for commented lines specifically by creating a
regular expression that looks for all lines that begin with a pound sign.
But this doesn't seem to be terribly useful.
We just see the commented lines that we see above.
However, combined with Grep's option to invert results, it becomes useful
by inverting. We tell grep to show us lines that don't begin with a pound sign
such as grep dash V
carrot pound sign at Z login depths.
And now we can see exactly what we wanted
settings actively used.
Now imagine how useful this would be in a very long file with hundreds of comments
that make it hard to spot what we're looking for.
And to show another example,
we could look for lines that start exactly with these four letters pass
to do that, we proceed our search pattern with the carrot sign
as in grep Carrot passed.
Now, let's imagine a different scenario.
Let's say we need to change a setting that is currently set to seven days.
Now, it's easy enough, we could look for a seven, right?
But this shows us some stuff we don't need.
However, we know that our file uses this syntax variable name followed by a space,
then a variable value,
the variable value is last,
which means that if some variable is set to have a value of seven,
this number will be the last character on that line
by using $7 sign.
We also added the dash W match word option to grep.
Otherwise another line would also match
one that has multiple sevens at the end.
But with dash W,
we essentially told grep to match only what contains a single seven digit at the end
to look for all lines that end with the text mail,
we would do mail dollar sign.
Now take note of how these operators are placed differently.
If you mix up their location,
you won't get any results which can lead
to confusion like why your regex isn't working
to easily remember the locations. Think about it like this.
The line begins with operator,
the carrot should be placed at the beginning of your search pattern.
The line ends with operator, the dollar sign goes at the end of your search pattern.
Now, anywhere you put a dot or a period in your expression,
it will match any character in that spot.
For example,
C dot T will match cat cut C RT and even C one T or C pound T,
but it won't match CT. There must be exactly one character between C and T
with C dot dot T. It will only match what has two characters in the middle.
And we can see here that even execute is
a match because that sequence fits inside the word.
Now, if we only want to match whole words with this red X and not parts of bigger words,
we can once again use Greps dash W option
and this brings us to an interesting problem.
Now, the period or dot has a special meaning in reject.
But what if we need to search for an actual period in our text?
This won't work
because this red
X will basically match each character one by one.
The solution however, is simple. We look for a regular period by escaping. This
escaping is how we tell our grep command.
Hey,
don't consider this period a match for any
one character operator instead treat it as a period
that to escape some special character, we just add a backslash before it.
So instead of the period, we write backslash period
and this is our grep command.
Now an expression like let Asterisk
Wool match le
let
le tt
and so on,
no matter how many T letters are at the end.
Another way of saying this is that the asterisk
allows the previous element to be omitted entirely,
appear once or appear two or more times.
And here's how we'd use it in a grep command.
The asterisk operator can be paired up with other operators, for example,
to look for something for sequences that begin with a front slash
and have zero or more characters between then end with another front slash.
We could use this since the period matches any one character. And it says
the previous element can exist 012 or many more times.
We basically allow any sequence of characters
to exist between those two front slashes.
Now let's say we want to find all sequences
of characters where zero appears one or more times,
we might be tempted to use something like this.
But this also matches lines that contain no zeros at all. And why is that
because the asterisk lets the previous character exist one or more times,
but also zero times, it basically allows that element to be optional in our search.
So we need another operator that forces the element
to exist at least one time or many more.
And the plus sign does that for us?
Now zero plus
would find strings like zero,
00000 and so on, we might think we can use this regular expression and grep like this.
But this doesn't look like the result we want
our plus sign works like a literal plus sign instead of an operator.
Why is that? Because by default grep uses basic regular expressions
and its manual page has this to say in basic regular expressions,
the metacharacters lose their special meaning.
Instead use the backslashed versions of them.
OK? So that means to use a plus operator here, we have to add a backslash before it
to make it backslash plus sign
and our command becomes this, but this can become confusing fast, right?
So we already saw something like backslash to
turn the period operator into a regular period.
Now we're using the backslash to turn a regular plus sign
into the plus sign operator.
It's really hard to keep track of what to backslash and what not to backslash.
So we can pick an easier route and use extended red X instead
which doesn't require us to backslash any of these characters.
And we'll explore that in our next lesson.