 Recently, I've made videos showing you guys the power of command line utilities like AUK and SID. Today I want to continue along with that journey. Today I want to show you two very powerful command line utilities. They are CUT and TR. So I'm going to switch over to my terminal and I'm going to show you guys the CUT command. What is the CUT command? What does it do? Well, it removes sections from each line of a file. That's essentially what it does. And when I say each line of a file, it could also be each line of some input that you direct into CUT. So you could pipe something into CUT. So let me show you a basic example of CUT. So I'm just going to echo something and pipe it into CUT. Let's echo, this is a line of text, exclamation point. And then I'm going to pipe that into CUT. And the most basic way that you use CUT is cut space dash C for characters and then one through 10. What this is going to do is it's going to go and grab the first 10 characters from each line of the input. Now, there's only one line of text in this case, but it goes and grabs the first 10 characters, including the spaces. If you wanted to see this on something that's multi-lined, I could cut dash C. One through 10 again, the first 10 characters from each line of, I don't know, how about my dot bash or C file? And that's how that would work. Let me clear the screen. So another thing you could do is instead of grabbing like the first set of characters here, one through 10, you can give it a character number such as 11 and then give it a dash, meaning grab every character starting at character number 11 to the end of each line. And that's how that would work. So the first command I did on bash or C, it grabbed one through 10 from each line and that second command grabbed characters 11 through whatever else was remaining on the line. One of the cool things that the cut command can do is it can go and grab certain fields from a line of text, similar to the way Alk does it. But this cut is kind of a poor man's Alk, really. It's not nearly as powerful, but some of the basic stuff you can do with the cut command as far as grabbing a certain field. Let me paste this line into the terminal. I'm going to echo, this is a line of text and then I'm going to pipe it into cut and I'm going to give it the dash D flag for delimiter. What is the field separator? What is the delimiter that separates each field? I'm going to say it's a space. So I have a space inside the single quotes. And then dash F for field number five, dash F five. I'm saying go and grab the fifth field based on spaces. So what would be the fifth field in this word? It would be the fifth word. It would be, this is a line of, of would be, of course, the fifth field. And of course, that field separator, that delimiter can be anything. It could be commas. If you were working with a file that was using a comma separated values, it could be a colon, which is something useful. For example, if I did a cat on my slash etsy slash pass WD file, you know, everything in this is actually separated by colons. There's no spaces. So what you would do for something like that is I would, I could cat that into cut, but I actually don't need to even cat into cut. Cut can take a file name. I'm going to do cut dash D and then inside single quotes. I'm going to put the colon for the delimiter. And then I'm going to tell it, grab the second field. And when I do that, the second field is all x's. And if I cat that again, you can actually see that x is actually the second field for every single line in this file. Now let me clear the screen. And one thing I should briefly mention is what is the difference between using cut, you know, as far as grabbing fields of text from lines compared to using something like awk. You got to understand cut really it takes a single character in dash D for the delimiter, meaning it takes a space, a single space as the delimiter or a single colon by default. If you don't tell cut what to use as a delimiter, it does a single tab as the delimiter. Now, awk is much more flexible because an awk, what you do is you give it a FS variable field separator variable and that can be anything. It doesn't, it's not limited to a single character. It can be an empty string. It could be a single character. It could be a regular expression, you know, whatever it is you want to use as that field separator. Another thing is awk also, it suppresses leading white space by default. So depending on whether that's something you want or don't want, that could affect your choice and whether you use cut or awk in some circumstances. Let me show you what I mean by that leading white space. So let me echo and let's do echo ABC space D E F. And then I'm going to pipe that into cut dash F2. Let's grab the second field and then dash D for delimiter and we'll just make a single space as the delimiter. And that works as expected. The second field of course is D E F. Now where things break down a little bit with cut or how they differ from awk is what if instead of ABC space D E F. I had ABC space space space D E F. Now, what is the second field? Well the second field is a space because it only treats one space as the delimiter and then the next space is actually the field. Now awk, this wouldn't trip up awk at all because if I did awk and then inside single quotes inside the braces, print, dollar sign two for the second field, it's going to give me D E F as expected. So that's just one tiny difference between cut and awk again. I'm just putting that out there so you don't get any unexpected consequences especially if you're scripting and you're using the cut command and you have leading white spaces and by leading white spaces also mean white spaces at the beginning of the line. If instead of the spaces being in the middle of the line, I had a situation where I had a space at the beginning here and I want to go grab the second field. The second field now becomes ABC because it actually treats that first space as a separator where if I did this with awk and added a leading white space, awk still gives me what you would expect which is D E F as the second field. Now let me clear the screen and let's go ahead and cover the TR command. What is TR? What does that actually stand for? Well, TR, a lot of people think it stands for trim because you can trim extra white space and things like that with it. Some people think TR stands for transform, like transform text. TR actually stands for translate. If I run a man on TR, TR translate or delete characters and that is actually a very important characters, not strings, not words. TR works on characters. So let's start with a basic example of TR. So I'm going to echo, this is a line of text and I'm going to pipe it into TR. And then TR takes two arguments. It takes, hey, what we're searching for, the character we're searching for, and then what we're replacing that character with. You can think of kind of like cut was a poor man's awk. You can think of TR in this case as a poor man's said or a poor man's awk because awk can also do some of what said does. But you get this output here where we replaced every lowercase a, there was only one with a capital A. Now remember, TR works on characters and how to really demonstrate this point, how to really hit it over the head is this here. I'm going to TR and this time instead of lowercase a with capital A, I'm going to do A, E, I, O. There's no use, I just did every vowel that was in the line, present in the line and I'm going to replace those with A, E, I, O. Capitalized and you see it's not looking for a specific string that is A, E, I, O. It is actually looking for characters. Think of this as almost mapping, right? It's mapping lowercase a to capital A, lowercase e to capital E, etc. If I run this, you see it replaced all the vowels, the lowercase vowels with capitalized vowels. Now other than translate, replacing these characters essentially is what it's doing. It can also delete characters. You can give TR the dash D flag for delete and it will go and find every A, E, I, O in this case and delete it from the line. And you see, we no longer have A, E, I, O, we have spaces still, which I guess I could specify delete every A, E, I, O and the space. And now we just get a bunch of consonants without any spaces or vowels. Now other than the standard search and replace kind of function or the delete kind of function, TR also has a really cool flag for squeeze and what this does is it will go and find repeated characters. Like if there was more than one A in a row, it will actually squeeze them down into being one A. So let me show you this. So if I go back into echo, this is a line. I'm going to do two vowels everywhere. I might do three A's in this case. I'll do a couple of I's, couple of E's. I'll do three spaces because why not? And then another space and then like five E's. And then TR dash, S, A, E, I, O, space. It's going to find every A, E, I, O and space. And where there were multiples of it is going to condense it back down to just one character. Let me hit enter and you see the output is the regular. This is a line of text. Now the TR command can take classes. So if instead of TR dash, S, A, E, I, O, I could also do this. I could do inside brackets. I could do colon, lower, colon, and then a space. And then inside single quotes, inside brackets again, I could do colon, upper, colon. If I hit enter, you see, I've translated all of the lower case letters into uppercase letters. And we've also squeezed them because we had the dash S flag on. Now I'm going to up arrow and rerun this command from earlier. I'm going to echo, this is a line of text. And then I'm going to do TR dash D for delete A, E, I, O. I told it to delete every A, E, I, O because I want to show you guys this flag dash C for complement D. So typically they're used together dash C, D for complement delete, meaning don't delete A, E, I, O. I want you to delete everything that wasn't an A, E, I, O. And you see how that works. And you'll notice it also deleted the trailing new line, because it's going to delete tabs, spaces, everything that is not A, E, I, O. If I wanted to have it respect the new line, I could, at the end of A, E, I, O, I could actually do backslash N, and now you see it doesn't mess up my prompt when I run that. Because now it outputs the A, E, I, O, and it also respects the new line at the very end. Now something cool you could do with dash C, D for the flags is a lot of people like to generate random passwords. You know, you want some kind of random string, or a long random string for a strong and complicated password, and one of the things a lot of people do is they'll pull that from slash dev slash u random. So if I did a head, grab the first 10 lines from slash dev slash u random, you know, that's the first 10 lines. That's actually a little bit too much output here. Why don't we do head, and let's just do three. So let's just get three lines. So I could use this as a very strong and complicated password. Well not exactly because you see all the weird characters, the weird boxes that have the question mark in it. Those are non-printable characters. Those are not real characters. But with TR, I can actually get rid of some of this stuff. Let me clear the screen here. So what I could do is I could take the head of slash dev slash u random, and I could actually pipe that into TR. And what I'll do is dash CD for compliment delete. And what do I want you to delete? I want you to delete everything that is not a printable character. So I'm gonna do inside the brackets colon print colon. And now it should return an actual normal string of printable characters, yes. So that is something I could actually work with right there. So that was just another example of using classes, kind of like you saw colon lower colon earlier, colon upper. Another thing you could do is colon digit colon. So maybe I wanna do this here, I'm gonna echo. This is a strong and complicated password. And then I'm gonna do dash one, two, three, four is my password, right? And well, I could pipe that into TR and I could do dash CD. And then inside single quotes, I'm gonna give it this. Brackets colon digit colon. So I want you to compliment delete everything that is not a digit. And it pulls out my strong and complicated password, which was one, two, three, four. Now, earlier in the video when I was talking about cut, I showed you some ways that cut and alt kinda resemble each other for some functions. I'm gonna do the same thing with TR and said because said actually can do some of what TR can do. For example, if I ran this command here echo, this is a line of text and I piped it into said and instead of S for substitute, I gave it Y for the said command. That is essentially using said kind of like TR for translate. So it's gonna map lowercase a to capital A, lowercase I to capital I, and if I ran that, that is the same as that very first echo command I ran with TR. Now, said and the Y function in said is not quite like TR. It can't do some of the things that TR does. For example, it does not handle classes. So if I wanted to do colon, lower colon, for example, and then do colon, upper colon here, what this is gonna do is it's gonna find every lowercase and replace it with uppercase. Well, that's what it would do with TR, but said doesn't handle that. You see, I get something completely different going on here. It's actually mapping each character. It's mapping the opening bracket to the opening bracket, the colon to the colon, and then L to U, O to P, W to P. And you can see L in line became U because that's what that character was mapped to. So it's completely different. You can't use colon, lower colon, upper colon, print or any of the classes. Also said Y doesn't have anything that resembles dash C with TR. It doesn't have that compliment delete mode. So that was just a little bit of the basics of the cut command and the TR command. I hope you guys are liking the series that I've been doing on some of these command line utilities. If you guys want me to continue to do these, please tell me in the comments below and also like this video. Now, before I go, I need to thank a few special people. I need to thank the producers of this episode. Absi gave James Mitchell, Paul West, Akami Allen, Chuck Kurt, David, Dylan, Gregory, Irion, Alexander, Peace Arch and Fedora, Polytech Scott, Steven and Willie. These guys, they are my highest tier patrons over on Patreon without these guys. This episode on the cut command and the TR command would not have been possible. The show is also brought to you by each and every one of these ladies and gentlemen as well. All these names you're seeing on the screen right now. These are all my supporters over on Patreon because I don't have any corporate sponsors. I just have you guys, the community. If you like what I do and wanna support my work, consider subscribing to Distro Tube over on Patreon. All right guys, peace.