Episode: 953 Title: HPR0953: LiTS 003: cut Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr0953/hpr0953.mp3 Transcribed: 2025-10-08 05:28:33 --- Welcome to Linux in the Shell Episode 3, the Cut Command. My name is Dan Washco, I will be your host. As always, if you have not read the website, I strongly encourage you to do so over at linuxinachel.org, either before or after listening to this audio, to solidify the command in your mind. I'd also like to give a shout out to Hacker Public Radio and thank them very much for hosting and all their support. Let's talk about the Unix Cut Command. It's one of the oldest commands available and it's a very simple command. What it does is it removes sections from each line of a file. Cut command can also be used to work on streams as opposed to just a single file. But we'll probably be mostly talking about working with a file. Now, it's very simple command. You can do some pretty powerful stuff with Cut, but it in no way does it approach the flexibility and power of a command like AUK, but we'll get to AUK in a later episode. So let's get back to Cut. Cut works on either fields, characters, or bytes. And the difference between those two is that a character and a byte are pretty much the same thing for most intents and purposes. Unless you're working on a multi-bites system, a character set, pretty much one character is going to equal one byte. Now I have seen the dash c, which stands for character, also called column. And I don't think that's right, but I've seen it written as column. But going back, it's always been character from what I can understand. So to reiterate, Cut works on characters, dash c, bytes, dash b, or fields, dash f. And so let's start at the most basic, the character. If you were to take a file, let's say the file has one line in it that says hello, how are you? And you were to cut dash c, one space, the file name. You're going to get the first character in that file. Now if you were to say dash c, one comma, two comma, three, you're going to get the first three characters in that file. Bite works the same way, dash b. So if you were to do cut dash b, one, you get the first byte, cut dash b, one comma, two comma, three, you get the first three bytes in that character. Now of course, like a lot of other commands that we've talked about in the past, instead of specifying each individual character field, you could also specify ranges. So instead of saying one comma, two comma, three, you can do one dash three. And that would specify the range of one to three. If you wanted to cut from a single point to the end of a file, you would just specify that point. For instance, let's say the 15th character to the end of the file, it would be 15 dash and you don't have to have an ending number. That would cut everything from the 15th character on. Now the reverse of that is also applicable. You want to cut everything from the beginning to the 15th character. It would be dash 15. So there wouldn't be a preceding number, it would just be dash 15. Now the other delimiter that I had not talked about yet is field. Field is a little different than character and bite in that you can specify the delimiter as opposed to just using characters and bites. Now what I mean by delimiter is, for instance, if you had a comma separated value file as CSV file, every entry in that file is separated by a comma. So the delimiter in there is the comma. Now sometimes it could be a tad character, it could be a colon, it could be a semicolon, it could be whatever you want it to be, but that's the delimiter. So you can specify the delimiter with a dash D. If you don't specify the delimiter, it defaults to tab. But anyway, the dash D specifies the delimiter and it works in conjunction with dash F, which is the fields. So what you define as the delimiter, and remember it's either with the dash D or tab by default, becomes what the file, the line gets cut on. So if you had a comma separated file with, say, a bunch of fruit names in it, like apple, pear, cherry, lemon, and you add each one of those separated by a comma, you would specify cut space dash D. Now I always put my delimiter in double quotes. So open quote, open double quote, comma, close double quote, and then space dash F and the fields that you want. Again, you can specify them individually, like one comma, two comma, three, or a range of one to three, or three, two to four, however you want to do it. And what that will do then is it will, the output would be the fields defined by, delimited by a comma, fields one, two, and three. So you would see apple, pear, and orange, I believe is what I said, or is if it was, you want to do two to four, it would be pear, orange, cherry, and you would have, that's how you work. Now, pay attention that when you're delimiting the fields, when you're cutting on the fields or bytes or characters, it doesn't start with zero, it starts with one. So the first character, first byte, first field is one, then two, and three, and so on, until the end. There are a few other flags or options that I would like to reference on the cut command. One of them is the dash compliment, or more accurately, dash dash compliment. But the dash, dash compliment flag does, is it does the reverse of what you're cutting, it takes the compliment of the set that you have cut. So if you specify that you wanted, and if you had a 15 characters in the file, you specified you wanting characters one through seven, you would get the first seven characters, but if you added the compliment to that compliment flag, you would get characters eight through 15, compliment, it takes the opposite of what you have selected. Now another flag is the output delimiter, which equals string, and allows you to change the output delimiter to something else. So for instance, if you had a stream of numbers, one, the seven separated by colons, and you specified the delimiter and cut dash D to be the colon, and then you want to fields two from five, it would output two, colon three, colon four, colon five. You still get the delimiter. Well you can change that delimiter by specifying dash dash output, dash delimiter, equals, and then some string. So for instance, you could say output delimiter equals comma, and instead of it being one, colon two, colon three, colon four, colon five, you would get one, comma two, comma three, comma four, comma five. So you can change the output delimiter on how it separates the characters in a field cut. The final caveat I want to say about using the dash F or field delimiter is that by default, when it comes to a line without any delimitors in it, it will display that line as it is. So if you had a comma separated file, and each line had a list of something separated by commas, except you had a line in there that was the sentence, hey, this is a great idea. It would output that whole line, this is a great idea, when it came to that line, because there's no delimitors on it, there's nothing to break it or whatever. So it will specify that as default, and you can, the dash S or the suppress option is what you can use, it's called suppress, but it's a dash S or dash dash only dash delimited will specify, it will only output lines with delimited characters in it. So to ignore that line, if that's what you want to do, so just be aware of that little caveat with fields. Now that's the entire cut command in a nutshell, right there. There really isn't much more to it than that. It's a simple, quick, dirty little utility that will allow you to cut up different pieces of either a stream or a file to be used in other ways. I use the cut command a lot with, sometimes with log files, where if I'm looking for something in a log file, that I will just do a rep and pass that to cut, because instead of getting a whole lot of output lines and everything, I'll just cut on the colon delimiter and get just the name of the file, I can do the cut, use the cut command, dirty to get the name of the file. I've used the cut command to chop up lines and scripts and stuff to get file names and make directories and stuff like that, and it's a pretty handy command. If you're ever thinking about, wow, I really need to cut something up here and take pieces of that string and do something with the different pieces of cut command might be the way to go. Simple, fast, and elegant. Thank you very much. I hope you enjoyed this entry. Go over to the website to read up on the cut command and also to watch the video. Thanks a lot, and we'll see you next time for another wonderful Linux command. You have been listening to Hacker Public Radio at Hacker Public Radio. We are a community podcast network that releases shows every weekday Monday through Friday. Today's show, like all our shows, was contributed by a HBR listener like yourself. If you ever consider recording a podcast, then visit our website to find out how easy it really is. Hacker Public Radio was founded by the digital dog pound and the infonomicum computer club. HBR is funded by the binary revolution at binref.com. All binref projects are crowd-responsive by linear pages. From shared hosting to custom private clouds, go to lunarpages.com for all your hosting needs. Unless otherwise stasis, today's show is released under a creative commons, attribution, share alike, free those own license.