1 00:00:00,000 --> 00:00:03,509 Hello everyone and welcome to the lecture of cut 2 00:00:03,509 --> 00:00:05,729 command, which is part of the text processor 3 00:00:05,729 --> 00:00:09,299 commands. What is a cut? A cut command is a 4 00:00:09,299 --> 00:00:11,909 command line utility that allows you to cut parts 5 00:00:11,909 --> 00:00:15,509 of lines from specified files or piped data, and 6 00:00:15,509 --> 00:00:17,729 print the result to standard output. It can be 7 00:00:17,729 --> 00:00:20,969 used to cut parts of a line by delimiter, by 8 00:00:20,969 --> 00:00:24,719 position, and characters. And the commands that we 9 00:00:24,719 --> 00:00:27,959 are going to learn are as follows. So there are, 10 00:00:28,109 --> 00:00:29,999 the first command that you can run is cut 11 00:00:29,999 --> 00:00:33,238 filename, again, do not run cut with just a file 12 00:00:33,238 --> 00:00:35,129 name because it's not going to work, you have to 13 00:00:35,129 --> 00:00:38,189 specify the option. So I will log into my Linux 14 00:00:38,189 --> 00:00:40,949 machine and this time, I'm going to log in 15 00:00:40,979 --> 00:00:44,399 through my PuTTY session. Right here, I have my 16 00:00:44,399 --> 00:00:47,999 Linux machine. And if you want to log into the 17 00:00:47,999 --> 00:00:50,909 PuTTY session, you have to log into your console, 18 00:00:51,209 --> 00:00:54,779 run ifconfig command, get your IP address, which 19 00:00:54,779 --> 00:00:59,399 is 192.168.1.162, and put into the PuTTY, and then you 20 00:00:59,399 --> 00:01:02,099 can log into through the PuTTY. But if you do not 21 00:01:02,099 --> 00:01:05,099 have this IP address, then you go to the Oracle 22 00:01:05,099 --> 00:01:08,369 VirtualBox network setting and you change the 23 00:01:08,369 --> 00:01:11,939 network setting to bridged adapter and then it 24 00:01:11,939 --> 00:01:16,019 will allows you to get this IP address. Anyway, so 25 00:01:16,019 --> 00:01:19,349 now the first command we'll use is cut and the 26 00:01:19,349 --> 00:01:21,749 file name, and which file name are we going 27 00:01:21,749 --> 00:01:27,869 to use. I have logged in as myself and I am in my 28 00:01:28,079 --> 00:01:31,289 Linux machine and which directory I'm in, I'm in 29 00:01:31,289 --> 00:01:33,569 home directory. In my home directory, I 30 00:01:33,570 --> 00:01:36,750 have a directory called seinfeld. I'll go into the 31 00:01:36,750 --> 00:01:39,690 seinfeld directory. Inside of that directory, I 32 00:01:39,690 --> 00:01:43,350 have a file named seinfeld-characters. And this is 33 00:01:43,350 --> 00:01:47,550 the file that I am going to use for this cut 34 00:01:47,790 --> 00:01:51,480 lecture. When you do cat on seinfeld-characters, 35 00:01:51,600 --> 00:01:55,380 you are going to see the list of all the lines 36 00:01:55,410 --> 00:01:58,260 inside of the seinfeld-characters. And these are 37 00:01:58,260 --> 00:02:01,620 the actual names of all the characters inside of 38 00:02:01,620 --> 00:02:04,920 the Seinfeld show. I don't want you to focus on 39 00:02:04,950 --> 00:02:07,830 the Seinfeld show itself, but I just want you to 40 00:02:07,830 --> 00:02:13,350 focus on the content of this file. If you do not 41 00:02:13,350 --> 00:02:17,040 have this seinfeld-characters file, please create 42 00:02:17,040 --> 00:02:20,850 it right now. Pause this video and you should, 43 00:02:20,910 --> 00:02:24,840 the file should have this content inside of your 44 00:02:25,560 --> 00:02:29,520 file seinfeld-characters. Anyway, so let's assume 45 00:02:29,640 --> 00:02:32,610 that you do have this file and you do have exactly 46 00:02:32,610 --> 00:02:36,630 all that content inside of this file. Now the 47 00:02:36,630 --> 00:02:39,990 first thing is you do cut seinfeld, and as I said, 48 00:02:39,990 --> 00:02:43,410 you cannot run cut without any options, it will 49 00:02:43,410 --> 00:02:46,110 give you the error message. Then the next command 50 00:02:46,110 --> 00:02:50,100 that you should run is the cut --version. So 51 00:02:50,100 --> 00:02:54,120 cut --version. And it will give you the 52 00:02:54,120 --> 00:02:57,150 version, right now I'm running the version 8.22, 53 00:02:57,270 --> 00:03:00,180 your version is probably new or the older it does 54 00:03:00,180 --> 00:03:02,880 not matter which version you have, the cut 55 00:03:02,910 --> 00:03:06,810 functionality is the same. Also, you could do cut 56 00:03:06,900 --> 00:03:10,140 --help to find information about each 57 00:03:10,170 --> 00:03:14,700 option that it has to offer. You could also do man 58 00:03:14,730 --> 00:03:18,780 cut to find detailed information about the command 59 00:03:18,810 --> 00:03:22,950 or utility cut. And you could go down by pressing 60 00:03:22,980 --> 00:03:27,390 the spacebar and hit Q to get out of it. The next 61 00:03:27,390 --> 00:03:30,840 command I want to show you is, if you want to get 62 00:03:30,960 --> 00:03:33,990 the first character or the first letter of the 63 00:03:33,990 --> 00:03:37,080 file seinfeld-characters. So let's see if you have 64 00:03:37,170 --> 00:03:40,710 seinfeld-characters file, let's cat it out. And 65 00:03:40,710 --> 00:03:45,090 you're gonna see that if you want to only get this 66 00:03:45,090 --> 00:03:48,420 first letter of every line, then you will run the 67 00:03:48,420 --> 00:03:52,110 command cut -c1 and the file name which is 68 00:03:52,110 --> 00:03:55,350 seinfeld-characters. And it will give you the list 69 00:03:55,350 --> 00:03:58,590 of all the first letters in every line. Then the 70 00:03:58,590 --> 00:04:01,140 next command we will run is, what if you want to 71 00:04:01,140 --> 00:04:03,840 pick and choose the characters. So let's do cat 72 00:04:03,840 --> 00:04:06,030 seinfeld again. And let's say if you want the 73 00:04:06,030 --> 00:04:08,910 first letter, then the third letter, and then the 74 00:04:08,910 --> 00:04:11,400 fifth letter, and then the eighth letter. Then you 75 00:04:11,400 --> 00:04:16,320 could do that by simply doing cut -c1 or 76 00:04:16,320 --> 00:04:19,800 three or five or seven, whichever characters that 77 00:04:19,800 --> 00:04:21,990 you want to pick and choose, and specify the file 78 00:04:21,990 --> 00:04:26,100 name. And in this case, it actually has pulled all 79 00:04:26,100 --> 00:04:29,310 those letters and jotted down together as one 80 00:04:29,340 --> 00:04:33,810 word. Then next one is a list, a range of characters. 81 00:04:33,810 --> 00:04:36,390 So what if you want to list the range of character. 82 00:04:36,390 --> 00:04:39,660 Meaning, let's do cat seinfeld again, and you want 83 00:04:39,660 --> 00:04:42,810 to list the first character all the way through 84 00:04:42,810 --> 00:04:45,480 the fifth character. Then you could do by simply 85 00:04:45,480 --> 00:04:50,550 doing cut -c sorry, -c1 through 86 00:04:50,550 --> 00:04:53,580 five or through three, whichever the range that 87 00:04:53,580 --> 00:04:55,980 you want to specify. You hit enter and you will 88 00:04:55,980 --> 00:04:58,860 see you will get the output exactly that will show 89 00:04:58,890 --> 00:05:03,360 only five letters. The next one is list specific 90 00:05:03,360 --> 00:05:07,230 range of characters. What if you want to, let's do 91 00:05:07,230 --> 00:05:11,430 cat seinfled, what if you want to do right here, one to three, 92 00:05:11,910 --> 00:05:16,800 and then six to eight, so from here to here. So 93 00:05:16,920 --> 00:05:22,874 it is as easy as we have done earlier. So cut -c1-3, 94 00:05:22,874 --> 00:05:27,760 then you put ,6-8, or 6-9, 95 00:05:27,760 --> 00:05:29,130 depending on your requirement, you 96 00:05:29,130 --> 00:05:31,800 hit enter, and you will see that it is giving you 97 00:05:31,980 --> 00:05:36,240 the first three letters, and then the six to eight letters. 98 00:05:36,255 --> 00:05:38,130 [No audio] 99 00:05:38,130 --> 00:05:41,520 The next example that we have is we could also get 100 00:05:41,820 --> 00:05:46,740 the list or output by the byte size. So if you do 101 00:05:46,740 --> 00:05:49,920 cat seinfeld, all of these letters are actually 102 00:05:49,920 --> 00:05:53,190 equivalent to the bytes as well. So byte pretty 103 00:05:53,190 --> 00:05:55,620 much works as the characters as well, or the 104 00:05:55,620 --> 00:06:00,640 letters. So if you do cut -b1-3, 105 00:06:00,640 --> 00:06:03,060 seinfeld, you'll see its output is 106 00:06:03,060 --> 00:06:06,630 pretty much similar or exactly, I would say, like 107 00:06:06,780 --> 00:06:11,580 the -c1-3. Anyway, the byte 108 00:06:11,640 --> 00:06:14,430 might change on a certain spaces. So just be 109 00:06:14,430 --> 00:06:17,400 careful what you want to use. Then the next one is, 110 00:06:17,400 --> 00:06:22,470 what if you wanted to get a list of columns of a 111 00:06:22,470 --> 00:06:26,220 file by specifying the delimiter. So what is the 112 00:06:26,220 --> 00:06:29,430 delimiter. So let's take an example of /etc/passwd file. 113 00:06:29,430 --> 00:06:32,097 So I want you to do cat /etc/passwd 114 00:06:32,097 --> 00:06:35,670 file, hit enter, and in this file, you 115 00:06:35,670 --> 00:06:38,250 will notice, this is the file by the way is 116 00:06:38,250 --> 00:06:42,600 used when you create a user in Linux, this file 117 00:06:42,630 --> 00:06:46,350 is the one that manages all the users or keep 118 00:06:46,350 --> 00:06:49,110 record of all the users. So that is something we 119 00:06:49,110 --> 00:06:51,600 will learn later, anyway, so in this file, if you 120 00:06:51,600 --> 00:06:55,830 notice, there are columns, and these columns are 121 00:06:55,860 --> 00:07:00,690 divided by this little colons. So you see this x 122 00:07:00,750 --> 00:07:03,570 and right after x, it has this colon, and then this 123 00:07:03,570 --> 00:07:09,150 column, colon, and column. So let's say I'm not 124 00:07:09,150 --> 00:07:11,610 going to use column, I will use field so it will be 125 00:07:11,610 --> 00:07:15,660 easier to understand. So colon, and field, colon, 126 00:07:15,720 --> 00:07:20,130 and field. So what if you wanted to get a field by 127 00:07:20,130 --> 00:07:23,370 specifying the delimiter and in this, in this 128 00:07:23,370 --> 00:07:27,750 case, the delimiter for you would be colon. So, if 129 00:07:27,750 --> 00:07:32,040 you want to do cut -d, and you specify 130 00:07:32,040 --> 00:07:37,020 delimiter, with colon, -f, and you specify 131 00:07:37,020 --> 00:07:41,730 the field six, then the name of the file which is 132 00:07:41,730 --> 00:07:45,450 seinfeld, you will see, oh, sorry, that's not seinfeld, 133 00:07:46,560 --> 00:07:50,130 /etc/passwd, /etc/passwd, then you will see 134 00:07:50,130 --> 00:07:53,070 it is giving you the sixth feild that is 135 00:07:53,070 --> 00:07:57,660 right after the colon. Let's do cat /etc/passwd again, 136 00:07:57,660 --> 00:07:59,880 then I could explain you, see, the 137 00:07:59,880 --> 00:08:05,910 first is this, second is this, third, fourth, 138 00:08:06,390 --> 00:08:11,940 fifth, sixth, and this is what it's grabbing for 139 00:08:11,940 --> 00:08:15,210 you. So in my case, if I have my user account, 140 00:08:15,210 --> 00:08:19,110 iafzal, this is my first field, second, third, 141 00:08:19,290 --> 00:08:24,270 fourth, fifth, and sixth. So this is my home 142 00:08:24,270 --> 00:08:27,120 directory. So six column is actually my home 143 00:08:27,120 --> 00:08:29,910 directory, so it is pulling that home directory. 144 00:08:29,910 --> 00:08:33,360 You could also use the delimiter on many different 145 00:08:33,360 --> 00:08:36,808 symbols as well, it's not only colon. If this file 146 00:08:36,808 --> 00:08:39,570 has a field that is separated by, let's say a 147 00:08:39,570 --> 00:08:42,600 comma, then you could use just like this comma, or 148 00:08:42,600 --> 00:08:46,320 if it was separated by let's say, question mark, 149 00:08:46,320 --> 00:08:49,440 then you will put question mark. Anyway, so with a 150 00:08:49,710 --> 00:08:53,250 colon, let's say if you want to pull the sixth and 151 00:08:53,250 --> 00:08:56,010 seventh field, then you can just specify six to 152 00:08:56,010 --> 00:08:58,740 seven, and hit enter, it will give you the two 153 00:08:58,740 --> 00:09:02,460 fields. Next one is, again, I already covered that, 154 00:09:02,760 --> 00:09:06,810 okay, and then the last one I have is the cut file 155 00:09:06,960 --> 00:09:10,830 not only works on the file itself, it also works 156 00:09:10,830 --> 00:09:14,970 on an output of a command. So let's say if you 157 00:09:14,970 --> 00:09:19,920 are in your home directory, /home/iafzal, and 158 00:09:19,920 --> 00:09:22,980 you do ls -l, you will see the list of all the 159 00:09:22,980 --> 00:09:26,550 files and directories. What if you want to get only 160 00:09:26,790 --> 00:09:30,240 these three bits. These three bits 161 00:09:30,240 --> 00:09:34,350 will, actually are the permissions of the file 162 00:09:34,500 --> 00:09:38,430 which is owned by the user. So if you want to get 163 00:09:38,430 --> 00:09:42,240 only that, then you will do ls -l, then cut 164 00:09:42,750 --> 00:09:47,760 -c2-4, right, so it will be 165 00:09:48,270 --> 00:09:52,830 two, three, and four. Hit enter and you will see it 166 00:09:52,830 --> 00:09:56,760 is only giving you that out. So that's how the cut 167 00:09:56,760 --> 00:09:59,190 command, and cut utility works. I have given you many 168 00:09:59,190 --> 00:10:02,250 examples, you should try it, and try it in a different way 169 00:10:02,250 --> 00:10:05,520 through it to a different files, or to different 170 00:10:05,550 --> 00:10:09,120 output, and it will be a lot of fun to work with it. 171 00:10:09,120 --> 00:10:11,370 If you have any questions please do send me a 172 00:10:11,370 --> 00:10:16,094 message, and I hope this lecture works well in the cut command. 173 00:10:16,094 --> 00:10:17,600 [No audio]