 Okay, today we're gonna be working on shell script tutorials. We're gonna be looking at the command called paste, which can do a lot of neat things. And we're just gonna look at a few of them here today. You can see I'm in a folder here with a couple of different text files. And we're gonna go over just three basic examples that you can do with paste. If you enjoy my tutorials and would like to see more, please think about contributing to my Patreon account at patreon.com forward slash metal x 1000. So first off, let's look at these example one and example two text files. So I can cat out example one. And you can see it's just a text file that has five lines and they're numbered one, two, three, four, five. Now if I cat out the second example file, example two, you can see it's five lines as well, A, B, C, D, E. So what we can do here is if we were to, let's say, want to line the first or each line of the first file with each line of the second file, we can just use the paste command, which you probably already have installed on your system. And I can say example one and example two dot text. And what this is gonna do is you can see it lines them up. Now, because these files are named pretty much the same, we can also use an asterisk character like this and just say all files to start with example, boom, it does that. So if the files are named different, you can type out the name of each file if they all begin with the same, you can use an asterisk like that. So we line them up, there is a big space there. So what we can do is we can tell it what to fill in between each character, a delimiter basically. So we're gonna run the same command, but we're gonna say dash D and give it a delimiter. And in this case, I'll say colon. And when we hit enter now, you can see that instead of a tab space, which I believe is what this is, yeah, it seems to be a tab space between each character, which is the default. So if you're trying to make a tab delimited file default, it would be fine. But let's say you want to do colons, you can do it like so. You can also do other characters. I can say dash and have dashes. Could probably do a period. Yeah, there you go. Some characters you might have to put in quotations. So let's say we wanted it to be like each one's numbered and we have a parentheses on one side there. We can just do that. This is not gonna work. I'm gonna get an error there because that's a special character that bash or your shell is seeing as a different, because I'm not using bash, I'm using zshell as you can see. But if we put it in quotations here, there you go. Now you have that as the delimiter. And yes, I probably titled this video as bash, but I do use zshell as my default. But actually, let's just go into bash just to make sure that everything works the same as I should since if I'm gonna title this a bash, there you go. Really shouldn't make much of a difference with the stuff we're doing today. Okay, so here we are. We did our first example with the example files. If I list out again, you can see that I have three files here labeled pay one, pay two and pay three text. So this is a scenario I came up with just to show you something else you could do with it. And this isn't really a very good real world example because if you're storing your files like this, you're not very bright, but we all start off somewhere. But let me cat out, pay one. You can see it has three lines. So just theoretically for this tutorial, let's say you're keeping track of your pay in separate files. The first line is how much you got paid this week. The second line is, let's say how much they take out in taxes. And because you're a good boy or a girl, third line is 10% that you put away for savings because we all should be saving. So that's pay one, pay two is the same, only you got paid a little bit less. So you save a little bit less, the less in taxes, three, you got paid a little bit more. So you can see where we're going with this. Okay, so now let's say you wanted to get a total from each week. What we can do here is we can say paste and we can say all our pay files like so. So they lined up, but what we can do is for our delimiter dash D, we can use a plus sign. So there you can see it put a plus sign. So now we're all ready to add it all up and we can put each line, we're gonna use a while loop and take each line and pipe it into the BC command, which is a calculator for your shell here. So what we're gonna do is we're gonna say while read line and hopefully you're familiar with while loops. I have used them all the time and I've done tutorials on them. But what we're saying here is read each line. So read this line, this line, this line and each time you read a line, put it into a variable we're gonna call line, but you can call it pretty much wherever you want and not done but do. And we're gonna say echo dollar sign line and we're gonna pipe that into BC and then we're gonna say done. So it's gonna loop and each time it's going to basically take each line here and add it up. We'll hit enter and there are your totals for each week. So over a three week period, you made $1,450. You had 285 taken out in taxes and you were able to save a whopping 145, not too shabby. So now let's make it a little bit, take it one step further. We can add into our while loop here. We can also say echo and we can say line and we can say equals. But when we have special characters like that, we probably wanna put this into the curly braces here. I've done a tutorial on that. Basically in some cases, one case where you might use curly braces is where a variable, you can't really tell one variable ends and a new string begins. So this is just saying, this is our variable. This is something else and let's not forget our closing quotation there. Hit enter and oh, so it did it, but it put the new, it does by default, echo does a new line character. So what we're gonna do is here dash and say no new line character and there we go. Let's clear the screen and run that again. There's your weekly totals and the sum. So it just makes it gives you a little more to read there, a little more output. Okay, so let's look at one more example and this is one that I can actually see happening and I can find paste being really useful for is if I list out here, I've got one last text file. It's called people. Let's say that you're very low text so you store all your contacts in a text file. I can tell you 20 years ago, I was doing this. So let's have a look at that text file. We'll cat out to people, oops, we spell it right. And you can see that I have a line with the person's last name, their first name, their address and a phone number and then a space and then we do it again, last name, first name, address, phone number and a space. And then a third person, last name, first name. So as long as we're formatted properly and each one has the same number of lines, we can use paste if we wanted to put these all on one line, maybe to prepare it to put it in a real database because you're ready to upgrade your system from using a plain text file that's not very well formatted into maybe a MySQL or even something like a CSV file so or whatever type of format you want that's better than this. So what we're gonna do is we're actually going to redirect the outputs, we're gonna say less than symbol here, the name of our text file and then we're gonna say paste and then we're gonna start doing these dashes and we want a dash for each line. So let's say I just did two there, you can start to see that it starts moving stuff around. Now we have four lines for each person but we also have to count this empty line. So we're gonna want five dashes, each with a space in between them. Now that we've done that, we now have a nicely formatted tab delimited file here that each person has their own line rather than having them on multiple lines. And I'm gonna try something that I didn't try before recording this tutorial and I don't know if this is gonna work but let's say you want to make this a comma separated file, we can say D comma for delimiter and maybe it does work and now you're pretty much ready as long as there's no other commas in the text file itself, you can import this right into a MySQL file or pipe it into more of a script where maybe you can create a JSON output or yeah, that'd be a good one there and either or you can import into most databases. So that's a look at the paste command very useful for certain things and I hope that these examples gave you an idea of where they may be able to help you. I know that probably about 10 years ago this blast option here would have come in real handy for me because like I said, I was saving stuff in text files kind of like I was up here but that was a long time ago. I don't do that anymore. So as always, I thank you for watching. Please visit FilmsByChris.com that's Chris the K. There should be a link in the description and as always, I hope that you have a great day. I would like to take a moment to thank the following patrons who made this video possible by supporting me over at patreon.com forward slash middle X 1000. And then decided that it was taking forever and let it keep running and before it finished running, he learned how to write a C program, ran it and he calculated the numbers in the bash script and do you remember this? Yeah, yeah, no, I think even, I wouldn't even see.