Hello the beautiful people.
And welcome back.
Now in this video, we're going to be discussing how you can take your sorting abilities to the next
level by looking at how you can sort data that is in tabular format.
So data that comes in tables by showing you how you can sort up and down various columns.
Now, this is super cool because it means that you can do all this kind of data sorting right from the
command line.
You don't need to open up a spreadsheet software.
You can do it right there in the terminal and save all that juicy, juicy processing power.
So let's go ahead and jump right into it and see how this works.
All right.
So sometimes you get a lot of data out of your commands.
And one example of this is the LZ command with the L option.
So the LZ Dash L command.
So if, for example, if we did that on the Z folder, so this is the folder that contains all of the
system configuration files.
And you can see that we just get a whole bunch of results out of this.
You just get loads and loads and loads of stuff coming out of there.
But just to make this a bit easier to see, let's just pass that into the head command and get the first
20 lines.
And there we are.
There's the first 20 lines of output.
Okay, so what if we wanted to sort this data by the columns?
So what if we wanted to sort by certain columns?
Now you can actually do that in the Sorte Command using the K option.
So for example, if we wanted to sort by, I don't know, I guess this column which is about the file
sizes, so you see this one here that this column is all about file sizes or maybe we want to sort by
date, for example, and we wanted to sort by date, which would be these few columns or whatever.
You know, you can do all that in the source command.
So let's just see how we might go about doing this.
So how we do it, if I just clear the screen is you'd pipe it into the sort command or the data into
the source command and you give it the K option.
Now after the K option, you need to tell the sort command how to sort this input using what the sort
command calls a key definition or a key def.
This is all in the main page, by the way, but let me show you how it works.
First, you tell it what column it should sort by.
Okay?
So if I just scroll up a little bit, you can see our results that we got before.
We want to sort by the column with these big numbers here.
Now, those numbers, as I said, are actually file sizes in bytes.
So let's say we want to sort our files from largest to smallest.
Okay.
So first, we need to give the key def, the column number we want to sort by.
So if we count here as column one, so that's one.
It's column two.
That's column three.
That's column four and that's column five.
Okay.
So the column number is five.
So if we come back to our new command line, we're going to type five, then you need to tell it, then
you basically can pass it any other options that we like.
So for example, if we want to sort numerically, we would type N and you can pass any options you like.
So if you wanted to sort reverse, you'd type an R there as well, just like you've seen in the last
video.
If you wanted unique you type a you and that's it.
That's how you make your key definition.
But let's just have the N for now.
So what we're saying here is, okay, list out everything that's in the Z folder, take the first 20
lines and then sort it by the fifth column.
And when we press enter, we'll see that.
Yes, actually, the data has been sorted by the fifth column.
You can see that we're starting with the smallest files first and working all the way up until the biggest.
But actually we wanted it the other way round, didn't we?
We wanted the biggest files first.
So if we wanted to make that happen, we'd just type an R there.
So we're saying sort numerically, but in reverse order.
So largest to smallest instead of smallest to largest, remembering that the sort command by default
goes small to big.
Now we're going big to small.
So when we do that, we see that, yep, now we're starting at big 4096 and getting smaller as we go
down.
So that's super cool, right?
Like we've just taken a big set of data and sorted it by columns, which is an amazing ability to be
able to just do from the command line.
Notice we didn't have to open anything like Microsoft Excel.
We just sorted the data straight from the command line.
But one thing that you might notice, especially with the LZ output, is that these file sizes, these
file sizes are kind of hard to interpret, right?
These are all in bytes.
And I don't know about you, but I tend to read things in kilobytes or megabytes or something like that.
I don't tend to read it directly in bytes.
So as you know, the LZ command has an option that allows you to read, you know, to give the data
in human readable format.
So if we do that, we do human readable format.
You can see now that what's happening is we're getting it's telling us the amount of kilobytes.
So it's saying this is four kilobytes, four kilobytes, four gigabytes, three kilobytes, 2.2 kilobytes.
And then the rest are just in bytes and it would say megabytes, gigabytes or whatever it was for the
files that we're dealing with.
So that's the height option for the LZ command.
It gives you human readable output, but you may have noticed that this is just completely messed up
our sorting.
Right?
It's just like before we were sorting perfectly, like we were sorting biggest at the top, smallest
at the bottom.
But now it's just doing whatever it wants.
And the reason is the Sorte Command, when you tell it to sort numerically with the end option, that's
a completely different thing to sorting with human readable data.
Okay.
So to make it actually sort with human readable data, you need to give it the hash option instead of
the N, you give it the hash option and you can't have both at the same time.
Okay.
So if I clear the screen and instead of having the end option, we now have the option and we press
enter.
Now you can see that it's working and we're getting the largest sizes at the top and the smallest at
the bottom while still getting the data in human.
Format.
So we get the best of both worlds, really.
So remember, if you want to sort by human readable data, you just need to give the sort command the
hatch option in its key def or just in general if you've only got one column to deal with.
But here we had it in a key def so we we type the option there now you can also sort by month so let's
see how we might sort by this column here which is all about the the date and the the file was last
edited.
So let's clear the screen and let's take a look here.
So we're saying print out list for us in long form format with human readable file sizes, everything
in the folder give us just the top 20 lines and sort for us by the fifth column.
Human readable, reversible.
Okay, reversed.
Well, actually, the the date is the column after this one.
So we need to have instead of the fifth column, we want the sixth column.
So we're going to do is in our key def, we're going to change the 5 to 6.
And instead of human readable, we're just going to put a capital M.
Now this is going to allow us to sort by month and if I press enter now you can see that yes we have
indeed sorted by month and we've got Jan at the top and we've got then May, August and then October
coming all the way down the bottom.
So that's really, really cool.
Right.
You can also as well combine this with oops, you can combine this with reversible by putting an R in
there.
And now we'll find that if I clear the screen and run it again, you'll see that October is at the top
and now January is at the bottom.
So, so there you go.
Like once you understand these several different options, it's really not that much more difficult
to sort by certain columns.
All you've got to do is sort.
It's just in the key def.
Tell it which column that you want.
Now here's a challenge for you.
I want to see whether you can tell me how you might sort by this column so that the you know, the smallest
numbers come first.
Okay.
So how would you sort by this column, which is just now has these numbers here, how would you sort
by that column so that the smallest numbers were first?
Go ahead and pause the videos now and then come back to see how you did.
How'd you get on?
Did you manage it?
Well, if you did, well done.
And that's really, really quick learning.
Here's how I would have gone about doing it.
Okay, so what I'd do is we'd keep everything at the same Liz with the Etsy taking out the top 20 lines
and now the only difference is our key def.
So the first question we know we've given the K option to the command.
What's the first question is what's the column number?
Well, the column number well, here this is the first column.
This is the second column.
Right.
So let's put two and we want to sort this numerically, right?
So we're going to sort it numerically.
Now by default, the sort command will sort smallest first, right?
So that means we don't have to reverse anything.
It will start with the smallest number first by default.
So if I clear the screen and try that, then now we see that it has been sorted by this column with
the smallest numbers coming first.
And if we wanted to reverse that, we'd just give it the N option and it would reverse that so that
now you're getting the eight at the top and the and the smallest numbers, the ones down the bottom.
So there's loads of different ways that you can you can do this, but there you are.
That's that's how you would use this sort command in order to sort a data in columns.
And as usual, one thing that I should note is that here we're having N and R, it doesn't matter if
I wrote that or an end.
It doesn't matter at all, like what order that you write stuff in these key definitions.
Just think of them as normal options that you just put right there in the key definition.
So order doesn't matter.
So there you have it.
We've covered a whole lot of stuff in the last two videos about sorting data, and in this video we
took a look at how you would sort data that is in a table format using the command line.
And you saw the key to this was to use the sort command with the K option.
Now you need to give the K option something called a key def and a key def follows like this kind of
structure.
So first you'll give it a number, and that number is the column that you're sought by.
So for example, we have the number three here and that means sort using column three.
Then after that, you can give it any other options that you want, like you've seen in the last video,
for example, you can give it the N option to sort numerically and the R option to sort in a reverse
manner.
So largest to smallest instead of smallest to largest and to source human readable data, you'd use
the H option, not the N option.
And those are incompatible.
You can't use both at the same time.
So if you've got human readable data, then use the option instead of the N option like you saw with
the RLS command that we used in this video.
Now you can also sort using months using the capital M option and because the sort commands sort smallest
to largest, you'll find that January comes first and then December comes last.
But of course you can reverse that using the R option as well.
And the order of options in the key def don't matter as usual, just like you were entering them normally
on the command line.
It doesn't matter if you put in and then R instead of r and then n, it doesn't matter the order.
But just make sure that you don't have incompatible options such as the numerical sort and the human
readable sort at the same time, because that won't work very well.
So you now know how to sort data using tabular sort tabular data using the command line.
And now you should be really the master of sorting data using the command line.
So well done you.
So in the next video, what we're going to do is we're going to take a look at an incredibly versatile
command called the grep command now because one thing that is super useful when working with the command
line is being able to search through data to find what we are looking for and pick out just the bits
that we're interested in.
Now, the grip command has got to be one of the most commonly used commands of all time, and it's going
to be such an awesome addition to your Linux toolkit.
You've probably already come across it in some form or some form or another if you've ever done some,
you know, searching around on the Internet for anything to do with Linux, you probably come across
the grep command.
So I can't really wait to show you this and show you how to use it properly.
So for all that good stuff, I'll see you in the next video.