We have already seen the use of the grep command, where we can go through and
search for text or regular expressions from files. We've seen the set command,
which can also search for information, but additionally edit information. Well,
the big brother to both of these commands is AWK. Now we can use it quite
simply to print a complete record.
We can search for vagrant, and print $0 will print that complete line.
However, we can take it a little bit further. Here we're using then the print
command to print out field 1, the username. We're then printing another line
tabbed in to show the user ID, field 3.
Now obviously we could take this to the nth degree, where we can print out
all of the fields tabbed in underneath our user, but let's go through and
just have a quick demo as an introduction to AWK, some of the things that
we can do. And set comfortably back at our command line, we can introduce
to you the command awk.
Now ‑F is our field delimiter. We're just using the colon in our passwd file.
We can go through and specify a regular expression. So we open up
our single quotes, and we can use the forward slash delimiters to
delimit our regular expression.
Now this will be text, or it can include operators like the caret, which means
the line begins with vagrant. With this in place, we can then open up our brace
brackets, indicating our main code. We're going to print $0, which is our
complete record or line that we match, close off our single quotes, and put in
our file name, and of course then we get the complete line that matches anything
beginning with vagrant.
I can change this to be $1. So if I go through and print $1,
instead of the whole line, we just get field 1.
Remember, we're using the delimiter of a colon here. To expand this
a little bit, we can have multiple print statements, so just use a
semicolon to separate our commands.
And we can go through and add in a little extra text if we want as well.
So we can go through and print, open up our double quotes, we're inside of
single quotes so I'm going to have to use double quotes, \t for our tab,
and then the text UID:, followed by a space, close our double quotes,
that's our static text. But we can combine this with a field number 3,
which is our user ID field, hit the big Enter key, and we should be able
to see then that the information shows our user, vagrant, and then the
fields that are tabbed in. Notice that I can display more information.
This will be easily done if I just go through, making sure that
I copy from the semicolon and paste that in again, and just
change to field 4, and the text then should read the group ID,
as field 4 is our group ID.
Now we can see then I've got a UID and group ID, and everything is kind
of nicely laid out for me. Now this is just showing you some of the very
simple stuff that AWK can do, but it does have its own quite intense
scripting language, which you can use from the command line or from
within your shell scripts.
But it does give you another topic to get excited about, but for the
moment we're going to move back to our slides so we might summarize just
a little of what we've looked at within this awesome module where we've learned so much about shell scripting.