- The next utility that I wanna talk about is awk.
So what is awk?
Awk is a great utility
for performing more complex operations on text.
It is using advanced features that make it
into a scripting language,
but these advanced features are not used that often anymore
but awk can make complex task more easier.
If you are going to try to find more information about awk,
you will find entire books that have been written
about awk.
Now, why is that?
That's because of the history of the command.
Awk was invented in a time that computers did not
have graphical screens next to them,
and working with computers was mainly line oriented.
In those days,
we needed very powerful tools to program text cells.
It's basically to automatically treat text cells
and to program manipulations into your text cells.
And that's where awk is coming from.
And likewise, where set is coming from.
That's the utility that is going to be up next.
The confusing thing about it is that whenever
you feel like,
Hey, I wanna know more about awk and about set,
then you will probably end up with one of these books
that are describing every single feature
of awk as well as set.
But the thing is, most of these features
are kind of not so useful anymore.
So what I've done, I've listed four examples for awk
and these are the examples that I want you to
understand.
Here on the slide, you can find them,
if you want to experiment for yourself.
Let me just demonstrate so
that we can see what they are doing.
So to start with, we have awk,
and then the awk operation.
The awk operation is between single quotes and curly braces
as we can see here.
So we have print dollar zero on ETC pass WD.
Now, what is that doing?
Well, that's just printing everything.
How about if you make that print dollar one,
that's also printing.
Is that everything?
No, it's not everything.
It's printing the first field,
but if you look closely,
then you can see it has a hard time
recognizing the fields.
So if you want to help awk a little bit,
and if you want to print a specific field,
then you need to add a field separator.
Minus F is that in an awk environment,
so if you use awk, minus F colon print dollar one,
then it knows that it needs colons as the field separator
and it'll be successful in finding this field.
Now let's do another manipulation and that's
awk length
dollar zero greater than 40 on ETC pass WD.
Now, what is that?
Well, it's looking for length dollar zero.
Dollar zero is the entire line.
And it'll only print if the length is greater than 40,
and you can see
that it limits the number of lines that we get.
If you even make it longer, let's make it 50,
you can see that the number
of lines is even getting shorter.
And how far can we go?
Let's try 70.
And there, you can see only a couple of lines.
So this is allowing you to do filtering criteria.
You only want to see lines that are longer than whatever.
This is what you wanna do.
Let's try to understand the historical background of that.
The historical background is that
if you have a text cell with lines longer than 40
then they might not print correctly.
So you might need to do an operation and split these lines.
And that is why a command like this is useful.
Okay. Last example with awk.
Let's print the entire contents of the past WDFL again,
so that I can explain.
If you look at the last line,
that is the line that contains the text Linda right here
This last line, then you can see that in past WD
we have a couple of fields.
The fields are separated by a colon.
Now it's field number three,
field number four, these are useful.
They contain the user ID
and the group ID, which are unique IDs.
And that's what I want to be looking for.
Now how can awk help us with that?
Well, I want to use awk to find the user ID for user Linda.
So how am I going to do that using awk?
Well, I am going to use awk minus F colon
and then I'm using my search pattern slash Linda slash
print dollar three, for instance, on ETC pass WD.
And what is that doing?
Well that is showing the user ID
for user Linda using awk,
and in case you're wondering
do we really need to use awk for that?
Well, of course not.
You can also use different commands to build it up.
Grep Linda on ETC pass WD, showing you the entire line.
And if we next use cut minus D colon minus F three,
then we get the same result.
That is something that you will frequently see
in Linux environments.
There are different solutions to get to the same result.
And if you think
that awk is too complex,
I can understand that.
Just use a combination
of utilities that you are more comfortable with
and you will get there as well.
And that's the most important lesson to be learned
about awk and also about set, which will be coming up next.
You can do pretty nice things with these utilities,
but you don't have to do it with these utilities.
You can do it in a different way.
That's fine as well.