[Autogenerated] And yes,
we're going to dive out to our command line now so we can start
working with text and regular expressions using sd.
We're not just restricted to only low line numbers that we've seen so far.
So we're going clone the co git repository.
So this is going to give us a little bit more complex
files that we're able to work with.
We're going to be looking at working with simple text and
extracting that filtering that out from our files before we start
looking at replacing text using substitutions.
Now, so far, when we've been looking at our comments,
they've been relatively simple whether line has begun with the comment.
But what as we've already pointed out if the line then is
indented to starting with white space characters,
how are we going to deal with that?
We're also going to see much as we did before using VM,
we can use line numbers, but we can also go to matching text with Vim as well.
So we can see how we can carry our ahead,
what we're learning into other utilities as well as just said.
So we've got a lot to look at, let's not waste any time.
And be sure you join me at the command line.
Ah Look at this, our glorious playground,
our playground though, we're not going to be playing.
We're going to be learning, this is learning.
We've got to remember that and we're going to learn how we can start
using text and regular expressions with our said editor.
Are you ready?
Well, I hope you're ready.
Come on, let's dive into this.
And one of the things we want to be doing is making sure that
we're working with some realistic files.
So for your convenience, we've added in a git repository,
I already have the git client installed so we can then go
through and clone the said an arc repository.
Now that's going to create a said an arc directory and that's
going to have the files that we need to work with.
So if I go through and take a look inside of the center,
an arc directory and the V host dot template,
this isn't an Apache, virtual host, template files.
I don't need you to install Apache,
but this just gives us a great file that gives us lots of things to work with.
As we can see, there are lots of comments,
but it's made a little bit more difficult by some of
those comments being indented.
So not starting with a comment but by starting with white space.
So we got to learn how to deal with that.
But to begin with, let's just learn how we deal with text.
So if I go through, say a look at sd suppressing my output,
nothing changes from what we saw before we can carry on using those
same characters and options that we used before.
We can open up our single quotes.
And we're going to then go through and specify that we
want to look for a regular expression.
Even though in our first example,
the regular expression is just going to be pure text.
But we delimit the regular expression with forward slashes.
So we're looking for server name,
we need to print the pattern match because we've suppressed our normal output.
And then we can close our said expression.
We can then go through and take a look in escape and dot to bring up
our last file name and that's the vs template.
And we can see then that our server name is _____ host dot example dot com.
Hm I'm guessing our server name probably is not _____ host dot example dot com.
So that could be something we wanted to change.
However,
let's investigate this a little bit further because I'm guessing that's not
the only _____ host dot example dot com in this file.
SSH, we have a look,
let's bring up our search again and let's go through and replace server
name and I can just copy and paste _____ host dot example dot com down and
put that as my regular expression to search for.
Now, when we go through and take a look at this you can see that we've got,
what, 68 lines or so quickly looking at the screen there.
Please don't tell me I'm wrong if I've counted them wrong.
But look, we've got eight lines or so where _____ host,
our example dot com has been written.
Now, of course, I could manually edit this,
but you've seen how bad my typing is.
Do you really think I could write the server name correctly?
Eight times?
I don't think so.
So of course,
there has to be a better way and that's where sd really can come in.
Let's go back and just clear our screen,
we can go back an our arrow key and we're now going to use a substitution,
we can then go through and say the text that we want to substitute.
That's still going to be in my pace buffer.
So I'm just putting that in _____ host dot example dot com.
That's our first string.
Then we pull our second string after the second forward slash and
we can put W W W dot example dot com and then close off that
string with the third forward slash.
So its also pressing our output and just printing those that have matched.
But look at this now that we're able to see our server admin server name,
document route all having the correct server name.
So we've now been able to edit these eight lines
really easily and really quickly.
How useful is this?
Now, of course, we've only been printing to the screen.
We haven't actually edited the file,
but it would be very easy for me to change the minus N to A
minus I if we were happy with our edit.
But another thing that we might want to do is clear out from our template,
those commented and empty lines.
So again, we can clear our screen,
move it to the top of the screen and then we can go through and use our
enhanced regular expression much as we used with grip.
We need to be deleting lines.
So we don't need to invert the expression.
We can delete lines which we weren't able to do with
re re app just displays lines,
we can specify that we want to look for lines beginning with
and then our enhanced regular expression,
the grouping that allows me to say the comment or the end of line marker.
Now it will have done it,
but we might not think so because we're still seeing commented lines.
Now,
we see they begin with a comment because we know there's
nothing other than spaces to the left, but that's it other than spaces,
computers are very literal.
So we have to change our regular expression a little bit,
but it's going to work for us across all of our files.
So if I go through and still leaving my line starts with,
so I've got the carrot, but after the carrot,
I'm going to put a back slash s the back slash S is
going to represent any white space character and that
could be a space or it could be a tab.
So I don't care how the spacing has been made,
but of course, it might be one space, it might be eight spaces,
it might be one tab, it might be two tabs.
So we put an asterisk after that, the asterisk means zero or more.
So it can begin just with a comment or a space and a
comment or two million spaces and a comment.
This is going to work.
And now when we execute it, look at this,
we've got a beautifully configured file.
Again,
we're only printing to the screen so we can make
sure that everything is correct.
But if everything is looking correct, we could edit the file.
But to be honest, we might find that keeping the comments is useful to us.
So we can actually just redirect our output and we can send it through
to dot V hosts dot template in my current directory.
So we still maintain the comments,
but we've got a working uncommenting vs file that gives us
a clean configuration that we can create should we need to
for a patchy virtual host?
And of course,
it's this file that we could look at editing because
it doesn't have those comments, the comments make great documentation,
to be honest, they don't make great configuration material.
Now, you may also recall that when we were looking at our line numbering,
we could use line numbering with vim.
So the question is going to be, can I use regular expressions with Vim?
Well, yes, you can.
And we delimit them in the same way as we do when we're
referencing a regular expression in instead we just put them
in between the two forward slashes.
So if we want to look for the server name,
we go plus forward slash server name forward slash and look at this,
we've been taken directly to the line that contained the
string server name that we were looking for.
So the real importance here is what we teaching you
is going to be useful with sd.
Yes, but its also going to be useful with other utilities.
As we saw before with Vim,
we're teaching you to be great Linux server administrators,
not just being able to use sd.
So hopefully you're getting really excited about all of the
goodies that are going to be following this.
So, come on, let's jump out of our file.
Once we've jumped out of the file,
we're going to be able to move on and start taking a look at other things
that we can start working with within our complex files.