Hello the beautiful people.
And welcome to this video where we're going to be learning about the super useful sword command.
Now, the sword command is one of those commands that you can kind of use anywhere to sort data and
make it more ordered and easier to manage.
And over the next few videos, I'm going to be showing you how you can use the sort command to sort
data in a variety of different ways.
And I'll also be showing you some use cases of when this can be really useful, and we'll be building
a super powerful pipeline that will allow us to sort data in tabular format.
So by the end, you're going to be amazing at sorting data and you'll have a really good understanding
of how to use the sort command to sort data in Linux no matter what, no matter whether it's text numbers,
modified numbers, or even in tables.
Ooh.
So this is going to be a very useful section of the course.
Let's go ahead and jump right into it.
Okay.
So the first thing I should probably say is that I've put three files on my desktop.
I've got words text which contains 100 random words sorted in no particular order.
They're just 100 random words that are just in that file.
And we've got the numbers 0 to 9 point text, which is a file that has loads of numbers in there.
I think 100 numbers each between zero and nine.
And we've also got this other file here called Numbers Text, which is, I think another hundred random
numbers.
But they don't they're in no particular range.
They're just 100 random numbers with no specific limit on them.
And I've made these files available in the resources section as well as some information about how I
created them.
So if you want to grab those and try this stuff out for yourself, then you can go ahead and download
them and give it a go.
Okay.
So let's start off by taking a look at this words txt, which is a file that contains 100 randomly generated
words.
And as I said, like if we just open that up again, it's just got 100 random words in there in no particular
order, no particular length, just 100 randomly generated words.
Okay.
So first of all, how can we sort that file so that it's sorted alphabetically?
So everything that starts with an A is at the top and everything that starts with a Z is at the bottom.
Well, that's actually the default behaviour of the sort command, which is the perfectly named command
for this kind of thing.
So it's really simple to use as well.
If we just type sort words, dot text that we've now got a list of sorted of sorted words from A to
Z.
So you can see that all the words that start with A at the top and all the words that start with Y are
at the bottom.
And it doesn't look like we've got anything that starts with a Z.
So they've all been sorted alphabetically.
So that's pretty cool.
And of course we can write this standard output to a file or to a T command.
We can use that as well.
So like we could say, say into the file here called sorted text and that's going to be output there
and we can pipe it down the pipeline.
You know, it's just written on standard output.
All the same rules apply.
So what if we wanted instead of sorting it A to Z, what if we wanted to sort it in reverse?
How would we sort it?
So Z was at the top.
Well, two options come to mind.
First of all, what you could do is you could just sort the words like just like normal, but then pipe
that into the tac command.
And as you can see here, what's happened is the letters, the words that start with a or now at the
bottom and the words that start with closer to Z are now at the top.
And the reason for that is that, as we know, the tac command will sort or flip output vertically.
So what would be at the bottom is now at the top and what was at the top is now at the bottom.
So that's effectively as though we've just reversed the sorting.
Right.
But a better way to do it, I suppose a more built in and efficient way to do it would be to just give
the sort command the R option and the R option stands for reverse.
So if I do that, we can see again we've got the letters starting with a R towards the bottom.
The word starting with a R towards the bottom.
Sorry.
And the words starting with close to Z are now at the top.
And now you may notice that when I'm showing you this output that I'm scrolling up and down the shell
quite a bit.
So this is where the less command would come in.
I could just pipe it into the less command to make the output a bit easier to view because now I'm using
the arrow keys instead of scrolling with my mouse pad.
So that's, you know, a bit of a nicer way to deal with things and compress.
Q to come out of that.
So just building upon what we did before, so awesome.
Notice how you have you now have enough building blocks that you can solve the same problem in multiple
ways.
You know that you could now use the R option to reverse it.
Or if you didn't want to do that, you could also pipe it into the tack command.
This is one great thing about Linux.
It gives you multiple different ways to solve the same problem because you've got so many different
building blocks, so there's plenty of room for creativity in the way that you approach these problems
is not necessarily one right way to do it.
So so there we are now.
How about sorting numbers?
Well, I, I have a list here of 100 numbers called numbers text.
And if we take a look, there's 100 numbers in there that don't have any particular size limit.
They can be any number, but there's 100 of them.
Okay.
So how would we sort those?
Well, sorting with letters.
So sorting eight A-Z sorting alphabetically is different from sorting numbers, which is sorting numerically.
So if we try to do sort numbers just like that, you see it didn't really work that well.
What kind of happened here is if you if you notice and we put this into the less command so we can make
it a bit easier to see, you know, so all the numbers that started with one have come to the top.
And then it kind of as you see now, we're getting all the numbers that start with two.
And then we'll get in all numbers that start with three and so on.
But we're not a four or five, six, seven, eight and nine.
You can see it's sorting by the first digit, but it's not sorting by the value of that number.
For example, 160 16,029 is much bigger than 1641.
So why is it above it?
Right.
There's no real sense of the size of the number.
It's just the fact that it starts with a number one and it's sorting numerically as it goes.
It's sorting as it goes along by just looking at the number.
It's not looking at the value of the number, it's just looking at the digits.
But if you want to sort using the value of the number, you need to give the sort command the N option.
So the RN option allows it to sort numerically.
When we take a look at that now, you can see that we're getting the smallest numbers at the top.
So we're starting with 123 and then the numbers are getting bigger as they go down.
So that's something important to bear in mind.
Sorting just by the digits is different than sorting by the value of the whole number.
And if you want to search by the value of the whole number, you need to give it the N option.
And of course, you can reverse this so that the biggest at the top by using the giving the R option
as well for reverse.
So now we see that the biggest numbers are at the top and as we scroll down, the numbers get smaller.
So that's very, very nice.
So you've seen how to sort text alphabetically and you've seen her two sort numbers numerically.
But one thing that's very useful is to only show unique results rather than the same result over and
over and over again.
So we've got a file here called numbers 0 to 9 point text.
And in there there's 100 rows of data, but only the rows only contain one of the numbers from 0 to
9.
So you'll necessarily have some duplicates.
For example, here we've got 000.
It's not just unique results.
There are results that are repeated.
So if we take a look in here and we try to sort that, which are just sort numbers 0 to 9.2, so let's
pipe it into less.
You'll see that all the zeros come first, then all the ones and all the twos and all the threes and
all the fours, the fives, the six, the 7/8 and the nines.
And that's that's a bit it's a bit annoying.
Okay, maybe we just want to just have the results once.
Well, to do that, what you do is you give the sort option sort command the you option and the you
option stands for unique.
And when you run that now you'll see that we only get the results once.
So let me just set up hoping that into less.
There you go.
You can see the you only get the results once so 0 to 9 and this is useful.
Sometimes you don't want to have the same result repeated over and over and over again.
You only want the results to appear once.
And that's why you would use the you option which allows you to sort uniquely.
And of course this still works with other options such as the reverse, which now we're sorting backwards
and you can still sort numerically and whatever else you might need to do.
But that's just how you can make sure that you only get each of the results once.
So in this video you've had a bit of an introduction to the Salt Command, and you've seen that you
can sort data using the sort command.
Now, the sort command tends to sort.
Smallest first.
So it'll tend to sort from A to Z or from 0 to 9 by default.
But you can reverse this by using the R option.
So the R option stands for reverse.
And if you ever sorting numerical data and you want the sort command to take into account the actual
value of the number rather than just the digits that it contains, then make sure that you give the
sort command the N option to allow it to sort numerically.
And if you only want to get unique results out of the sort command, provide it with the U option which
will filter out just the results that appear once.
So that's what we covered in this video.
But in the next video, we're going to take it to a next level and show you some advanced sorting techniques
that will allow you to sort data that comes in table format.
So and we're also going to be building a really cool pipeline that will do some of this as well.
So let's just go ahead and get right into it in the next video and take your sorting skills to the next
level.