In this video, you'll learn about some common text tools.
First, let's talk about LESS.
Is LESS really more?
So how about it?
Well, MORE was the original pager.
When we say pager,
then we're talking about utilities that were
developed to show the contents of a text file page by page.
LESS was developed to offer some more advanced features.
Sometimes in the open source community,
people are funny, so they wanted
to program LESS to do more than MORE.
As a reaction to that,
MORE was developed a bit more.
But still, to do MORE, use LESS.
Does it make sense? Yeah, really, it does make sense.
Because in LESS, you can easily scroll forwards,
as well as backwards in files,
and in MORE, you cannot.
For that reason, you better use LESS.
Now, LESS is a pager.
We have seen it occasionally already.
It's very simple, very intuitive.
Press the space bar to move forward,
press Q to get out,
and use forward slash text to look for certain text.
Sometimes you don't want to see the entire file.
You just want to see a couple of lines in the files.
Then you would use HEAD to
show the first 10 lines of a file, for instance.
TIL is doing the opposite.
It will show you the last 10 lines.
As argument, you can also use MINUS followed by a number
to specify any other number of files.
And you can even combine it in smart combinations,
like HEAD minus 10 ETC PASS WD PIPE
TIL minus 1, which is doing what?
Well, it's showing the last line of the result,
HEAD minus 10, which means that it will show
the 10th line in ETC PASS WD,
which, by the way, is a configuration file
that contains usernames.
There's more utilities, like CAT,
which dumps text file contents on screen.
CAT can use a couple of options, like MINUS UPPERCASE A,
which shows all non-printable characters,
which can be convenient if you have a text file
that has a pretty strict syntax
and it doesn't work out the way you want it to.
MINUS B will number lines,
and MINUS S will suppress repeated empty lines.
And probably one of the most useless commands on Linux
is TEK. It's doing the same, but in reverse order.
It really sounds a little bit like an exercise
in a base course of C programming,
but maybe it's convenient.
Who knows?
So that's already quite a few commands.
Let me demonstrate before we continue.
So let me start with LESS on var log messages.
Var log messages is the old way
how information was logged on Linux
and it still exists on RHEL 10.
You'll learn more about it later in this course.
Now, if I want to look for something, I can search in it.
So slash SSH, for instance, is looking for the text SSH,
and look at that. It's working.
And what also works is the UP arrow.
That's something that you can easily do with the LESS utility.
Now, you can also view it using CAT,
but CAT doesn't do the page-by-page thing,
so you only see the last couple of lines in var log messages,
and that is not very convenient.
Now, talking about var log messages,
TIL minus F is a useful command.
The minus F is for follow.
So TIL minus F on var log messages
is going to follow new output as it appears.
So when I'm opening a new window,
then in my new window, I can see immediately.
I don't know if you've noticed here
that systemd run has started the new user bin bash shell.
But hey, TIL minus F, how do we get out of here?
Because I don't get my prompt back.
Well, you remember from the bash keyboard shortcuts,
Control-C is a generic way to get out of there.
Now, do I have anything else that I can show you?
Well, I probably do.
Let's do a CAT on anaconda-ks.cfg,
which is the kickstart configuration file.
And CAT is dumping the contents on my screen.
And how about CAT minus A?
There we have all the non-printables.
So you see the dollars?
The dollars are indicating end-of-line characters,
and that can make it easy to find hidden tabs
or hidden spaces or whatever.
And a final example, let me create a file
with the name numbers, where we have 1 and 2 and 3.
And this command is so useless that it
does deserve its own demo.
There we go, tech on numbers 3, 2, and 1.
And CAT on numbers, well, obviously,
that is 1, 2, and 3.
That's all for now.
Let's talk about some more utilities
that you can use to filter and process text files.
So CUT is a commonly used utility to filter output.
You use it like CUT minus D colon minus F1 on ETC pass WD,
where minus D is identifying the delimiter.
That is how fields are separated in ETC pass WD.
And minus F is identifying the field that you want to see.
SORT is sorting output.
You can use CUT minus D colon minus F1 ETC pass WD SORT,
and that would work.
But you can also use the SORT utility.
So in SORT, it's minus T colon, because in SORT,
it's not the delimiter, but the training character.
And then minus K3N, which is filtering numeric
based on the third column in ETC pass WD.
And finally, there is TR for translate,
where you can do an echo hello, pipe TR colon lower colon
to colon upper colon, which is translating
lowercase to uppercase.
Let me show you.
So when I would use CUT minus F1 ETC pass WD,
what is the problem?
Well, the problem is that CUT knows
that it needs to look for fields,
but it has no idea how to find the appropriate field.
So I need to add this minus D colon option.
And there we can see that it is filtered out column number 1
from ETC pass WD.
And if you want that to be sorted,
well, put a pipe behind it to send it to the SORT command,
and then you've got it alphabetically sorted.
Now, in ETC pass WD, the third field contains the user ID.
Let's do a quick cut on ETC pass WD.
And the convention is that right here, we have used a student.
We have used Olinda.
And what we care about is users with a user ID
higher than 1,000.
That's typically for the people user.
And the funny thing is that you can use SORT to filter
on that specific field.
So SORT minus D colon minus K3N, which
is doing a numeric SORT on ETC pass WD third field.
And here we can see all these user IDs in the right order.
Can be convenient, this sorting on a specific field
in the output.
Well, you still see the entire line.
And that's something that CUT can't do.
CUT would just cut out the line while losing
all of the other context.
Finally, there is TR.
So how do we use it?
Echo hello.
Great, then.
TR colon lower colon upper, which is translating lowercase
to uppercase.
And there you can see suddenly we have it all in uppercase.
TR is not so useful by itself.
It is mainly useful in shell scripts,
where you sometimes want to treat all characters
in the same way.
So you convert everything to an uppercase,
or you convert everything to a lowercase.
This is the way how it works.
Now we can do something funny as well.
Echo hello pipe TR.
And what do we want to TR?
I want to TR AK to LZ.
And there we can see what it has done.
So it has translated all in the lower character
sets to a corresponding character in the higher
character set.
Obviously, the L didn't match, but the other ones did match.
Is that useful?
No, not at all.
But sometimes you should be interested in funny parts
of the commands as well, isn't it?