Introduction to awk scripting

Narendra P
A free video tutorial from Narendra P
Automation Engineer with Scripting Languages and Tools
4.5 instructor rating • 5 courses • 39,090 students

Learn more from the full course

Complete Bash Shell Scripting

Automate repetitive tasks with Bash Shell Scripting to save valuable time

18:20:28 of on-demand video • Updated November 2020

  • Students will be able to understand shell scripting concepts
  • Students will be able to understand unix filters like awk, cut and sed
  • This course is helpful to Automate repetitive tasks in different Admin areas like Linux Admins, Middleware Admins, Database Admins, DevOps Admin and AWS Cloud Admins
  • This course is helpful to write basic to advance level shell scripts
English [Auto] Parents, in this video we are going to discuss about Ebola case gripping the two, simply very important to your airplane, right? OK. Anyway, from our previous videos, we know what is the basic command syntax. Right. So, you know, that is like some options then pattern or condition. Then you want action, then filename ANA in the syntax. You know, you are providing input from filename so you can also provide input for your command in this way. Some command output you can do as input for you replicate by using pipeline. Right. And in this right. This action is going to perform on this file or on the command output. Based on. Either pattern or condition, I suppose, if you are using pattern syntax, then if pattern is matching, then the action is going to perform on your record of your fine. So by default, action is going to perform on each and every line of your input, but. In case if you mentioned either partner condition, then if partner is matching or condition is satisfied, then only the action is going to perform on the input of your file. Right. See, let me give you one simple example. So I suppose if you see cut through the password file, you are having so many records on your file, that means record number of lines like. No, if you simply look, this is your comment, I'm not using any options, simply I am using that brain. Dr. Jérome. Then our input file, then what what is your action? Just write the foreword printed each and every record from this file which print each and every line from this file. Yes. Now, what I am doing is. I'm going to mention some bottom. Right now, you're going to get only those two lines, because in those two lanes only you have route matching worth. So this is based on Padrón supples. I want to display the records. Based on condition, my condition is. If record number is greater than our records to some three. Now, see? Like, oh, let me print record numbers so that he can get it. See, only from three yards playing, because condition is greater than or equal to three, so from third line, I want to split all the lines. That means so based on either pattern or condition. If you don't write a pattern or condition, your error is going to look based on this action on each and every line. That's fine. This is about simply your advocate and you have some options for your application already we have seen in our previous videos, Affinia for Life and We Options. Now we have one more option that is funny and this is helpful in your outlook. Could being part. Right, see, the thing is nothing but including two more actions along with this action now, whatever you are having to get the action part along with this, you are going to add two more actions before this one action and after this one action. Right now, see that the syntax for stripping AWG begin or start action, then your actual action here. This means this actually is going to perform on your input, then end or stop action. Then you are filename might see sometimes to write this lengthy logic, whatever the logic you have inside of your quotation, single quotations. Right. That is relentlessly sometimes if it is a lengthy logic, instead of writing this logic on your command line, you can also write that logic in one file and you can call while learning you're able to get that. File in this way, and I if you actually whatever the logic you are having on that logic, we are going to write in this file, then you are filing so you can use a straightforward syntax or if you feel your logic is lengthy for unobligated, then you can write your logic in one file with the extension as not enabling your file. And you can call that file in this way while in your comment. So finally, Captain, Syntaxes AWG begin or start action, then this is the actual action then or relection then end or stop action. So you have to remember this syntax, so instead of writing this logic here, what we are doing, if that logic is lengthy, then you are writing that logic into one file and you're calling that file using this hyphen option. Right on here, he had to know big block or begin starting action is performed before reading the file. So you know that from your look at command, whatever the action you are having here, that is going to perform on each and every line of your input. But big in action, no, it is not going to do anything with your input. Before reading the file, you are whatever the logic, you how inside of your biggin, inside of this culebras is that we execute in a straightforward way. Right now, and the bloc in the bloc is performed after crossing the file, that means after completion of this logic. At the end, that is the last step at the end you are going to execute this and the. Let's say suppose you have some 10 lines in your file. So this logic is going to perform an election or general election? Election is going to perform on each and every line of these 10 lines. Once this action on trend lines is completed, then the block will execute. Like, so start actionis before going to start your actual action on your input file and ended after completion of your actual action on your input file. So begin and end remaining is your actual action or your action to perform your fight like rest of the actions are performed while passing the fight? That's OK while working with of dream. Remember some points. They are. Yet needs at least one action to run Arabic script, you can use either this action now or this action or this action, if you mention at least one action, then there is a use with the Arabic. Otherwise there is no use. Let me show you that. Supples. So there is no much difference between your comment on the script only, including the ending block that you now see, I'm not writing any action, simply I'm providing empty single quotations actually inside this yard. Right. Your actual logic. I'm an actual action or begin audience. All those things. We are trapped inside of these single quotations right on your command line. Then let me provide some point. There is no output because there is no action. No. Right. So need at least one action to run your script then? No need of input for inaction. Let's assume that you are having only begin action up to this you how a single quotation. Then this logic, then you are completing your single quotation here. That means you are skipping this logic all the time. You need to provide any input. Let me show you that. Supples and. Like. You have to mention Biggin. Then simply writing print. OK, that's a close. Even if you provide input, no problem, even if you don't provide for your vehicle, only if you have been active in your area like a command, it will work without having any input. But for remaining at this action and action, you need input. Right now, let me write a simple script by including all your begin action and end actions begin. See what I'm writing here, print. Working on in D.C.. Password file. So this is my biggest action before going to work with your file, using your advocate. This is what I want to execute, then actually actual logic. What I need is the lines which are matching with the route where those lines only I want to print. Right then. End what is end after completion of your logic, I want to simply print completely word action. Completed work on to D.C. password file. That's it, then provide your input, whatever the input, actually, we are going to take password file now. See that? Before starting, you were actual this is your actual logic, right? This is now you are getting out because of this logic. But before this, I want to print something then that you can read in your beginning of the completion of this. I want to print something not only printed. Can I add some logic also? Well, going forward, we will see that. Now you're getting that. So this is because of the end and this is because of a blog, I mean, begin blog, and this is your actual because of your action. So this is your beginning. Then you are getting this outport. And this is your actual logic and you're getting this output and this is your end block by block logic. Because of that, you are getting this, OK, so this is very simple about your script, right? So. Well, going forward. Yeah, we have one more thing right. I finish this option. Let me show you that as well. See, what I'm writing is so whatever the logic you how Yermo inside of your single quotation, right. Let me copy that. My daughter is stooped, so I'm going to copy and paste that logic here and I'm going to rearrange in a clear way. So that is your big impact. This is my actual logic, right, then you end and also right. No, no, I mean indentations, but it's better to provide but don't provide quotations, no knowledge of quotations when if you are going to write your logic in a script. Now, if and if you are an oblique. Then. File, that's. OK, this is one of the way to write your script and how to execute that right so they can also write by mentioning Shabangu. We will see that way going forward. Right before going to right any obligation to remember this standard tax? OK, OK, thank you for watching this video.