1 00:00:00,150 --> 00:00:01,410 Hello the beautiful people. 2 00:00:01,410 --> 00:00:02,440 And welcome back. 3 00:00:02,460 --> 00:00:07,080 Now in this video, we're going to be discussing how you can take your sorting abilities to the next 4 00:00:07,080 --> 00:00:11,100 level by looking at how you can sort data that is in tabular format. 5 00:00:11,100 --> 00:00:16,230 So data that comes in tables by showing you how you can sort up and down various columns. 6 00:00:16,230 --> 00:00:21,240 Now, this is super cool because it means that you can do all this kind of data sorting right from the 7 00:00:21,240 --> 00:00:21,660 command line. 8 00:00:21,660 --> 00:00:23,550 You don't need to open up a spreadsheet software. 9 00:00:23,550 --> 00:00:28,500 You can do it right there in the terminal and save all that juicy, juicy processing power. 10 00:00:28,500 --> 00:00:31,230 So let's go ahead and jump right into it and see how this works. 11 00:00:31,950 --> 00:00:32,790 All right. 12 00:00:32,790 --> 00:00:36,180 So sometimes you get a lot of data out of your commands. 13 00:00:36,180 --> 00:00:40,050 And one example of this is the LZ command with the L option. 14 00:00:40,050 --> 00:00:42,300 So the LZ Dash L command. 15 00:00:42,300 --> 00:00:46,920 So if, for example, if we did that on the Z folder, so this is the folder that contains all of the 16 00:00:46,920 --> 00:00:48,630 system configuration files. 17 00:00:48,690 --> 00:00:51,510 And you can see that we just get a whole bunch of results out of this. 18 00:00:51,720 --> 00:00:54,180 You just get loads and loads and loads of stuff coming out of there. 19 00:00:55,440 --> 00:01:00,330 But just to make this a bit easier to see, let's just pass that into the head command and get the first 20 00:01:00,330 --> 00:01:01,320 20 lines. 21 00:01:01,560 --> 00:01:01,980 And there we are. 22 00:01:01,980 --> 00:01:04,800 There's the first 20 lines of output. 23 00:01:05,190 --> 00:01:10,200 Okay, so what if we wanted to sort this data by the columns? 24 00:01:10,200 --> 00:01:12,780 So what if we wanted to sort by certain columns? 25 00:01:12,820 --> 00:01:17,610 Now you can actually do that in the Sorte Command using the K option. 26 00:01:17,610 --> 00:01:21,930 So for example, if we wanted to sort by, I don't know, I guess this column which is about the file 27 00:01:21,930 --> 00:01:27,360 sizes, so you see this one here that this column is all about file sizes or maybe we want to sort by 28 00:01:27,360 --> 00:01:32,160 date, for example, and we wanted to sort by date, which would be these few columns or whatever. 29 00:01:32,160 --> 00:01:34,650 You know, you can do all that in the source command. 30 00:01:34,650 --> 00:01:36,990 So let's just see how we might go about doing this. 31 00:01:37,380 --> 00:01:42,540 So how we do it, if I just clear the screen is you'd pipe it into the sort command or the data into 32 00:01:42,540 --> 00:01:44,850 the source command and you give it the K option. 33 00:01:44,850 --> 00:01:51,060 Now after the K option, you need to tell the sort command how to sort this input using what the sort 34 00:01:51,060 --> 00:01:53,670 command calls a key definition or a key def. 35 00:01:53,970 --> 00:01:57,510 This is all in the main page, by the way, but let me show you how it works. 36 00:01:57,690 --> 00:02:01,380 First, you tell it what column it should sort by. 37 00:02:01,410 --> 00:02:01,670 Okay? 38 00:02:01,800 --> 00:02:05,880 So if I just scroll up a little bit, you can see our results that we got before. 39 00:02:06,000 --> 00:02:09,780 We want to sort by the column with these big numbers here. 40 00:02:10,380 --> 00:02:14,400 Now, those numbers, as I said, are actually file sizes in bytes. 41 00:02:14,400 --> 00:02:18,600 So let's say we want to sort our files from largest to smallest. 42 00:02:18,630 --> 00:02:18,960 Okay. 43 00:02:19,110 --> 00:02:23,340 So first, we need to give the key def, the column number we want to sort by. 44 00:02:23,340 --> 00:02:25,740 So if we count here as column one, so that's one. 45 00:02:25,740 --> 00:02:27,060 It's column two. 46 00:02:27,060 --> 00:02:28,680 That's column three. 47 00:02:28,680 --> 00:02:32,670 That's column four and that's column five. 48 00:02:32,670 --> 00:02:32,930 Okay. 49 00:02:33,030 --> 00:02:34,890 So the column number is five. 50 00:02:34,890 --> 00:02:42,180 So if we come back to our new command line, we're going to type five, then you need to tell it, then 51 00:02:42,180 --> 00:02:44,850 you basically can pass it any other options that we like. 52 00:02:44,850 --> 00:02:50,370 So for example, if we want to sort numerically, we would type N and you can pass any options you like. 53 00:02:50,370 --> 00:02:54,570 So if you wanted to sort reverse, you'd type an R there as well, just like you've seen in the last 54 00:02:54,570 --> 00:02:54,720 video. 55 00:02:54,720 --> 00:02:57,330 If you wanted unique you type a you and that's it. 56 00:02:57,330 --> 00:02:58,680 That's how you make your key definition. 57 00:02:58,680 --> 00:03:00,600 But let's just have the N for now. 58 00:03:00,600 --> 00:03:05,310 So what we're saying here is, okay, list out everything that's in the Z folder, take the first 20 59 00:03:05,310 --> 00:03:09,150 lines and then sort it by the fifth column. 60 00:03:10,530 --> 00:03:13,050 And when we press enter, we'll see that. 61 00:03:13,050 --> 00:03:15,660 Yes, actually, the data has been sorted by the fifth column. 62 00:03:15,660 --> 00:03:20,460 You can see that we're starting with the smallest files first and working all the way up until the biggest. 63 00:03:20,460 --> 00:03:22,950 But actually we wanted it the other way round, didn't we? 64 00:03:22,950 --> 00:03:24,690 We wanted the biggest files first. 65 00:03:24,690 --> 00:03:28,170 So if we wanted to make that happen, we'd just type an R there. 66 00:03:28,170 --> 00:03:30,900 So we're saying sort numerically, but in reverse order. 67 00:03:30,900 --> 00:03:36,330 So largest to smallest instead of smallest to largest, remembering that the sort command by default 68 00:03:36,330 --> 00:03:37,740 goes small to big. 69 00:03:37,740 --> 00:03:39,240 Now we're going big to small. 70 00:03:39,240 --> 00:03:46,080 So when we do that, we see that, yep, now we're starting at big 4096 and getting smaller as we go 71 00:03:46,080 --> 00:03:46,710 down. 72 00:03:46,710 --> 00:03:47,970 So that's super cool, right? 73 00:03:47,970 --> 00:03:53,700 Like we've just taken a big set of data and sorted it by columns, which is an amazing ability to be 74 00:03:53,700 --> 00:03:54,780 able to just do from the command line. 75 00:03:54,780 --> 00:03:57,600 Notice we didn't have to open anything like Microsoft Excel. 76 00:03:57,600 --> 00:04:00,900 We just sorted the data straight from the command line. 77 00:04:01,380 --> 00:04:07,050 But one thing that you might notice, especially with the LZ output, is that these file sizes, these 78 00:04:07,050 --> 00:04:09,070 file sizes are kind of hard to interpret, right? 79 00:04:09,090 --> 00:04:10,320 These are all in bytes. 80 00:04:10,320 --> 00:04:14,100 And I don't know about you, but I tend to read things in kilobytes or megabytes or something like that. 81 00:04:14,100 --> 00:04:16,890 I don't tend to read it directly in bytes. 82 00:04:16,890 --> 00:04:24,270 So as you know, the LZ command has an option that allows you to read, you know, to give the data 83 00:04:24,270 --> 00:04:25,860 in human readable format. 84 00:04:25,860 --> 00:04:28,080 So if we do that, we do human readable format. 85 00:04:28,080 --> 00:04:32,100 You can see now that what's happening is we're getting it's telling us the amount of kilobytes. 86 00:04:32,100 --> 00:04:36,690 So it's saying this is four kilobytes, four kilobytes, four gigabytes, three kilobytes, 2.2 kilobytes. 87 00:04:36,690 --> 00:04:40,260 And then the rest are just in bytes and it would say megabytes, gigabytes or whatever it was for the 88 00:04:40,260 --> 00:04:41,310 files that we're dealing with. 89 00:04:41,340 --> 00:04:43,470 So that's the height option for the LZ command. 90 00:04:43,470 --> 00:04:47,850 It gives you human readable output, but you may have noticed that this is just completely messed up 91 00:04:47,850 --> 00:04:48,660 our sorting. 92 00:04:48,660 --> 00:04:49,110 Right? 93 00:04:49,110 --> 00:04:53,790 It's just like before we were sorting perfectly, like we were sorting biggest at the top, smallest 94 00:04:53,790 --> 00:04:54,360 at the bottom. 95 00:04:54,360 --> 00:04:56,610 But now it's just doing whatever it wants. 96 00:04:56,610 --> 00:05:02,790 And the reason is the Sorte Command, when you tell it to sort numerically with the end option, that's 97 00:05:02,790 --> 00:05:07,320 a completely different thing to sorting with human readable data. 98 00:05:07,350 --> 00:05:07,750 Okay. 99 00:05:07,770 --> 00:05:12,750 So to make it actually sort with human readable data, you need to give it the hash option instead of 100 00:05:12,750 --> 00:05:16,290 the N, you give it the hash option and you can't have both at the same time. 101 00:05:16,290 --> 00:05:16,590 Okay. 102 00:05:16,710 --> 00:05:21,330 So if I clear the screen and instead of having the end option, we now have the option and we press 103 00:05:21,330 --> 00:05:21,780 enter. 104 00:05:21,780 --> 00:05:27,300 Now you can see that it's working and we're getting the largest sizes at the top and the smallest at 105 00:05:27,300 --> 00:05:31,290 the bottom while still getting the data in human. 106 00:05:31,710 --> 00:05:32,100 Format. 107 00:05:32,100 --> 00:05:34,270 So we get the best of both worlds, really. 108 00:05:34,290 --> 00:05:39,030 So remember, if you want to sort by human readable data, you just need to give the sort command the 109 00:05:39,030 --> 00:05:44,760 hatch option in its key def or just in general if you've only got one column to deal with. 110 00:05:44,760 --> 00:05:51,120 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 111 00:05:51,120 --> 00:05:58,140 see how we might sort by this column here which is all about the the date and the the file was last 112 00:05:58,140 --> 00:05:58,770 edited. 113 00:05:58,770 --> 00:06:01,650 So let's clear the screen and let's take a look here. 114 00:06:01,650 --> 00:06:08,310 So we're saying print out list for us in long form format with human readable file sizes, everything 115 00:06:08,310 --> 00:06:14,520 in the folder give us just the top 20 lines and sort for us by the fifth column. 116 00:06:14,820 --> 00:06:16,420 Human readable, reversible. 117 00:06:16,440 --> 00:06:17,400 Okay, reversed. 118 00:06:17,400 --> 00:06:22,410 Well, actually, the the date is the column after this one. 119 00:06:22,410 --> 00:06:25,740 So we need to have instead of the fifth column, we want the sixth column. 120 00:06:25,740 --> 00:06:29,130 So we're going to do is in our key def, we're going to change the 5 to 6. 121 00:06:29,400 --> 00:06:33,270 And instead of human readable, we're just going to put a capital M. 122 00:06:33,810 --> 00:06:39,750 Now this is going to allow us to sort by month and if I press enter now you can see that yes we have 123 00:06:39,750 --> 00:06:47,280 indeed sorted by month and we've got Jan at the top and we've got then May, August and then October 124 00:06:47,280 --> 00:06:48,870 coming all the way down the bottom. 125 00:06:48,870 --> 00:06:49,740 So that's really, really cool. 126 00:06:49,740 --> 00:06:50,160 Right. 127 00:06:50,700 --> 00:06:55,320 You can also as well combine this with oops, you can combine this with reversible by putting an R in 128 00:06:55,350 --> 00:06:55,770 there. 129 00:06:55,770 --> 00:07:00,510 And now we'll find that if I clear the screen and run it again, you'll see that October is at the top 130 00:07:00,510 --> 00:07:03,090 and now January is at the bottom. 131 00:07:03,090 --> 00:07:05,250 So, so there you go. 132 00:07:05,250 --> 00:07:10,260 Like once you understand these several different options, it's really not that much more difficult 133 00:07:10,260 --> 00:07:12,270 to sort by certain columns. 134 00:07:12,270 --> 00:07:14,190 All you've got to do is sort. 135 00:07:14,400 --> 00:07:15,480 It's just in the key def. 136 00:07:15,480 --> 00:07:16,840 Tell it which column that you want. 137 00:07:16,860 --> 00:07:18,270 Now here's a challenge for you. 138 00:07:18,540 --> 00:07:25,110 I want to see whether you can tell me how you might sort by this column so that the you know, the smallest 139 00:07:25,110 --> 00:07:26,580 numbers come first. 140 00:07:26,590 --> 00:07:26,750 Okay. 141 00:07:26,850 --> 00:07:31,200 So how would you sort by this column, which is just now has these numbers here, how would you sort 142 00:07:31,200 --> 00:07:33,510 by that column so that the smallest numbers were first? 143 00:07:33,510 --> 00:07:37,440 Go ahead and pause the videos now and then come back to see how you did. 144 00:07:43,270 --> 00:07:43,840 How'd you get on? 145 00:07:43,870 --> 00:07:44,650 Did you manage it? 146 00:07:44,950 --> 00:07:46,660 Well, if you did, well done. 147 00:07:46,850 --> 00:07:48,610 And that's really, really quick learning. 148 00:07:48,910 --> 00:07:50,680 Here's how I would have gone about doing it. 149 00:07:50,790 --> 00:07:59,710 Okay, so what I'd do is we'd keep everything at the same Liz with the Etsy taking out the top 20 lines 150 00:07:59,710 --> 00:08:01,660 and now the only difference is our key def. 151 00:08:02,080 --> 00:08:05,080 So the first question we know we've given the K option to the command. 152 00:08:05,080 --> 00:08:07,160 What's the first question is what's the column number? 153 00:08:07,180 --> 00:08:09,130 Well, the column number well, here this is the first column. 154 00:08:09,130 --> 00:08:10,330 This is the second column. 155 00:08:10,330 --> 00:08:10,690 Right. 156 00:08:10,690 --> 00:08:15,640 So let's put two and we want to sort this numerically, right? 157 00:08:15,640 --> 00:08:17,170 So we're going to sort it numerically. 158 00:08:17,170 --> 00:08:21,370 Now by default, the sort command will sort smallest first, right? 159 00:08:21,370 --> 00:08:23,290 So that means we don't have to reverse anything. 160 00:08:23,290 --> 00:08:25,970 It will start with the smallest number first by default. 161 00:08:25,970 --> 00:08:32,260 So if I clear the screen and try that, then now we see that it has been sorted by this column with 162 00:08:32,260 --> 00:08:34,270 the smallest numbers coming first. 163 00:08:34,270 --> 00:08:39,280 And if we wanted to reverse that, we'd just give it the N option and it would reverse that so that 164 00:08:39,429 --> 00:08:46,330 now you're getting the eight at the top and the and the smallest numbers, the ones down the bottom. 165 00:08:46,330 --> 00:08:50,050 So there's loads of different ways that you can you can do this, but there you are. 166 00:08:50,050 --> 00:08:56,560 That's that's how you would use this sort command in order to sort a data in columns. 167 00:08:56,560 --> 00:09:02,830 And as usual, one thing that I should note is that here we're having N and R, it doesn't matter if 168 00:09:02,830 --> 00:09:04,030 I wrote that or an end. 169 00:09:04,060 --> 00:09:08,470 It doesn't matter at all, like what order that you write stuff in these key definitions. 170 00:09:08,470 --> 00:09:12,970 Just think of them as normal options that you just put right there in the key definition. 171 00:09:12,970 --> 00:09:14,680 So order doesn't matter. 172 00:09:15,740 --> 00:09:16,760 So there you have it. 173 00:09:16,790 --> 00:09:21,530 We've covered a whole lot of stuff in the last two videos about sorting data, and in this video we 174 00:09:21,530 --> 00:09:26,570 took a look at how you would sort data that is in a table format using the command line. 175 00:09:26,570 --> 00:09:30,360 And you saw the key to this was to use the sort command with the K option. 176 00:09:30,380 --> 00:09:35,810 Now you need to give the K option something called a key def and a key def follows like this kind of 177 00:09:35,810 --> 00:09:36,410 structure. 178 00:09:36,410 --> 00:09:41,270 So first you'll give it a number, and that number is the column that you're sought by. 179 00:09:41,270 --> 00:09:46,220 So for example, we have the number three here and that means sort using column three. 180 00:09:46,220 --> 00:09:50,840 Then after that, you can give it any other options that you want, like you've seen in the last video, 181 00:09:50,840 --> 00:09:55,820 for example, you can give it the N option to sort numerically and the R option to sort in a reverse 182 00:09:55,820 --> 00:09:56,210 manner. 183 00:09:56,210 --> 00:10:01,910 So largest to smallest instead of smallest to largest and to source human readable data, you'd use 184 00:10:01,910 --> 00:10:04,340 the H option, not the N option. 185 00:10:04,340 --> 00:10:05,540 And those are incompatible. 186 00:10:05,540 --> 00:10:07,220 You can't use both at the same time. 187 00:10:07,220 --> 00:10:13,610 So if you've got human readable data, then use the option instead of the N option like you saw with 188 00:10:13,610 --> 00:10:15,920 the RLS command that we used in this video. 189 00:10:16,100 --> 00:10:21,840 Now you can also sort using months using the capital M option and because the sort commands sort smallest 190 00:10:21,840 --> 00:10:26,060 to largest, you'll find that January comes first and then December comes last. 191 00:10:26,060 --> 00:10:29,510 But of course you can reverse that using the R option as well. 192 00:10:29,510 --> 00:10:34,040 And the order of options in the key def don't matter as usual, just like you were entering them normally 193 00:10:34,040 --> 00:10:34,520 on the command line. 194 00:10:34,520 --> 00:10:39,170 It doesn't matter if you put in and then R instead of r and then n, it doesn't matter the order. 195 00:10:39,530 --> 00:10:44,240 But just make sure that you don't have incompatible options such as the numerical sort and the human 196 00:10:44,240 --> 00:10:47,480 readable sort at the same time, because that won't work very well. 197 00:10:47,480 --> 00:10:53,690 So you now know how to sort data using tabular sort tabular data using the command line. 198 00:10:53,690 --> 00:10:58,010 And now you should be really the master of sorting data using the command line. 199 00:10:58,010 --> 00:10:59,090 So well done you. 200 00:10:59,360 --> 00:11:03,890 So in the next video, what we're going to do is we're going to take a look at an incredibly versatile 201 00:11:03,890 --> 00:11:08,330 command called the grep command now because one thing that is super useful when working with the command 202 00:11:08,330 --> 00:11:14,270 line is being able to search through data to find what we are looking for and pick out just the bits 203 00:11:14,270 --> 00:11:15,290 that we're interested in. 204 00:11:15,290 --> 00:11:19,490 Now, the grip command has got to be one of the most commonly used commands of all time, and it's going 205 00:11:19,490 --> 00:11:22,190 to be such an awesome addition to your Linux toolkit. 206 00:11:22,220 --> 00:11:26,990 You've probably already come across it in some form or some form or another if you've ever done some, 207 00:11:27,080 --> 00:11:31,160 you know, searching around on the Internet for anything to do with Linux, you probably come across 208 00:11:31,160 --> 00:11:32,120 the grep command. 209 00:11:32,120 --> 00:11:35,300 So I can't really wait to show you this and show you how to use it properly. 210 00:11:35,300 --> 00:11:38,210 So for all that good stuff, I'll see you in the next video.