Time
8 hours 30 minutes
Difficulty
Beginner
CEU/CPE
10

Video Transcription

00:00
welcome to Section 103.7.
00:03
We're gonna look at regular expressions a little bit again.
00:07
We've already touched on some of these commands earlier, like said and grab.
00:11
But there's also some other options, such as E grap or Grab Dash E and F Grab, which is great dash F.
00:21
Now this.
00:21
The group command gives us lots of
00:25
easy to use functions for
00:28
searching through files for different matching bits of text.
00:33
For instance, I can ignore the
00:35
the case of something I'm searching for. By using Dash I.
00:39
I can count the number of matches with Desi
00:42
Group. Dash V
00:45
tells me to match everything that, except for what I specified.
00:49
So that's kind of a neat thing. Its way to filter out matches.
00:53
Show me the non matching lines.
00:55
I could display line numbers
00:58
and get a little bit of more information about what I'm
01:03
trying t o find from a particular file.
01:08
So let's look at an example here
01:11
if I do a great dash I for the word error
01:15
and our log messages.
01:18
Actually, I got a copy of that locally here,
01:23
quite a few things show up
01:30
it's gonna get for this other file. All right,
01:34
now, how many lines actually had the word error in it?
01:40
What I could do
01:42
is count those.
01:45
So I'm gonna count and still ignore case. I know. I got 49 lines with the word error.
01:52
It's pretty handy to, uh, to see that. Now, How many lines would I have if I count those that do not contain the word error? Quite a few more. Right. So that shows me that
02:02
that's the rest of the file. Whatever doesn't match this particular
02:07
item.
02:07
And I can also
02:09
display the line number where the matches occur.
02:14
Oops.
02:22
I think you just need to do and bites out there ago. So there is my line numbers on the left side.
02:30
And that might be useful because maybe I want to know exactly where in the file these lines are located, because when the match happens, it just sends them to standard output without any regard for where they occurred within the file.
02:43
And this set example
02:46
shows Maybe I've got a word that I want to change. So I can say in this example,
02:52
capital user or lower case user replaced with user one
02:57
in a particular,
02:59
uh, text file.
03:00
It's another example of how we can use regular expressions.
03:06
You saw this table earlier with some of the regular expression details.
03:09
Zero or more matches, one or more matches,
03:13
digits, words beginning of the end of the line
03:15
and rangers of characters.
03:19
So if we look at some of these examples
03:24
lips,
03:30
I want to look for the word
03:31
ah, particular word, let's say on the word route
03:37
in a certain file,
03:38
I can use the carrot to match at the beginning of the line.
03:46
Now, if the route if if the If I ran this without matching at the beginning of the line,
03:52
I'm still gonna get a match. But it's gonna match in other places as well as you can see.
03:58
So it depends on what you're trying to accomplish.
04:00
If I try to search for roots at the end of the line,
04:04
I'm not gonna get anything because it probably
04:08
and should not exist in their file like password.
04:11
However,
04:12
I could change this to bash,
04:16
and
04:17
now I can see that I'm matching the log in shell for a couple of my
04:23
users on this particular system
04:26
by being able Thio match at the end of line. I couldn't found those files are those lines in that file in other ways? But I'm on it.
04:32
More specifically say Look at the beginning and look at the end.
04:38
Um,
04:39
maybe I also want to
04:42
use egress, which is like Grab dashi, which says, Use regular expressions.
04:46
I'm gonna search at the beginning of the line
04:49
for character range. Let's say a through D. I want to find
04:55
all instances of
04:58
words at the beginning of the line that have this range of characters
05:04
we can see. I've got
05:06
a three D in these
05:10
beginning of the line items. So these air user accounts or log ins and around this system that start with those letters
05:16
and you get an idea for how powerful this these tools are.
05:21
Another great example. I can search for
05:27
digits that are certainly
05:29
so zero through nine,
05:32
and I'm going to use the currently braces to put a number in here,
05:39
and that should show me all digits that air four digits or greater
05:43
are all numbers rather than a four digits are greater.
05:46
I can do this with letters as well.
05:49
If I wanted to see all words that had
05:55
four letters in four consecutive letters together, I could do that.
05:58
So pretty, pretty interesting now, um,
06:02
grip, dash F or otherwise called F grab.
06:09
This allows me to
06:11
search for fixed characters.
06:14
So if I had a
06:18
a particular string I want to search for that might have
06:21
controlled characters embedded in it
06:24
I could search for It
06:27
helps if you spell it right.
06:30
Here we go. So there's
06:32
if there might be new line characters at the end of each of these. But I can search for that text anyway,
06:39
and I can even specify
06:42
if I had a file.
06:48
Sorry, I created a file
06:50
called Users Dot Taxed. It just has a couple of user names in it
06:56
so I can
06:58
do a grip dash looking for literal characters,
07:00
and I could use something like an input file.
07:06
And now it takes
07:08
these these values in the file, including whatever new characters are new line characters, maybe their character turned characters
07:14
and still allows me to do the search within the target file.
07:20
So these kinds of commands are a little bit tricky to get used to, but with a little practice you can get more familiar with how to
07:28
do the basics of regular expression matching beginning and end of line and things that are in the middle of the line.
07:32
And if you're looking for through error logs and things of that, nature is really helpful techniques to have.
07:41
So that gets us to the end of the regular expression section. Next will look at V I
07:47
So you, then thank you.

Up Next

CompTIA Linux+

Our self-paced online CompTIA Linux+ course prepares students with the knowledge to become a certified Linux+ expert, spanning a curriculum that covers Linux maintenance tasks, user assistance and installation and configuration.

Instructed By

Instructor Profile Image
Dean Pompilio
CEO of SteppingStone Solutions
Instructor