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 • 23,474 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 September 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] Hyphens. In this video we are going to discuss a bold italic is crippling not to simply very introduction but to your analysis. Right. OK. Anyway from our previous videos. Right. We know what is the basic end of like a Command syntax right. So you know that is like looking at some options than pattern or condition then you would action. Then finally in the syntax you know you are providing input from filing so you can also provide input for your et obligate command. In this way some command output you can do as input for your end of the game 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. Suppose if you are using pattern syntax then if pattern is matching then the action is going to perform on your record of your file so by default action is going to perform on each and every line of your input. But in gears if you mentioned either pattern or condition then if pattern is matching or condition is my 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 you suppose if you see a cat who did this see a password file you're having so many records on your file that means the government's number of lines like no. If you simply EAW W.K. This is your command. I'm not using any options simply I am using that brain daughter Jill then your input. Fine. Then what. What is your action. Just tried to forward brain to each and every record from this file. Let me bring each and every line from this file. Yes now what I am doing is I'm going to mention some bottom right now see the output you're going to get. Only those two lines because in those two lines only you how route matching were right. So this is based on pattern. Suppose I wondered display the records based on condition my condition is if a record number is greater than audit close to some three let's see that right. Let me print record numbers so that he can get it see only from three you are displaying because condition is greater than or equal to three. So from third line I want to display all the lines that means. So based on either pattern or condition if you don't write either pattern or condition you are italic is going to work. 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 rate of decay. And already we have seen in our previous videos hyphen year for knife and V options. Now we have one more option that is hyphen. And this is helpful in your looks keeping by right. See you guys. The biggest thing is nothing but including two more actions along with your this action. Now whatever you are having in your rate of looking at the action part along with these you are going to add two more actions before this one action and offer this one action right. Now see that the syntax for adblock is stripping AWB begin all start action then you are actual action here. This means this actually is going to perform on your input then end or stop action then you are finally right. See sometimes to write this line the logic Whatever the logic you how inside of your quotations single quotations right. That is the lengthy logic sometimes if it is lengthy logic in July of writing this logic on your command line you can also write that logic in one file and you can call while learning or anybody care that file in this week and obligate hyphen if you could actually. What was the logic you're having on that logic we are going to write in this file that you are filing so you can use a site for this syntax or if you feel your logic is lengthy readable again then you can write your logic in one file with extension as not anybody give file and you can. All that filing this way way learning your advocate. Come at right. So finally you Luke is keeping syntax is a W.K. begin all start action then this is actual action then automobile action then end or stop action so you have to remember first. 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 we are calling that file using these hyphen option right on here you have to know that big in block or begin starting. Action is form before reading the file C you know that from your edit look at command whatever the action you are having here. Note that is going to perform one 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 filing you are whatever the logic you how inside of your begin inside of this curly braces that will execute in a straightforward way right now and block and the block is perform after crossing the file that means after the completion of this logic at the end. That is the last hope at the end you are going to execute this and the logic lets say suppose you how some end lines in your file. So this logic is going to perform middle action audio or deflection actual action is going to perform on each and every line of these 10 lines. Once this action on 10 lines is completed then the book will execute right. So start action is before going to start your actual action on your input file an end is often a completion of your actual action on your input file. So begin and end. Remaining is your actual action or your action to perform you are fighting like the rest of the actions are performed while passing the file. That's okay while working with the enemy escape your dream remember some points that are yet I'm looking needs at least one action to run and escape. You can use a that this action now or this action or this action if you mention at least one action. Then there is a use with this idyllic escape. Otherwise there is no use. Let me show you that suppleness so that there is no much difference between here and look at my nondescript only including beginning and ending block. That's it. Now see here W.K. I am not writing any action simply I am providing empty single quotations. Actually inside this yard write your actual logic. I an actual action or begin audience. All those things we are right inside of the single quotations like on your command line. Then let me provide some file. There is no output because there is no action no output right. So need at least one action to run your end of Lucas script. Then no need of input for begin action. Let's assume that you are having only begin action up to this you how it obligate single quotation. Then this logic then you are completing your single quotation here. That means you are skipping this logic at the time you need to provide any input. Let me show you that. Suppose Adam McKay you had to mention begin than simply am writing print. Okay. That's it close even if you provide input. No problem. Even if you don't provide for you or begin only if you how big an action you read like a command it will work without having any input. But for remaining this action and action you need input right. Now let me write simple of guest group to by including all you to begin actual and end actions right. Begin. See what I'm writing here. Print working on code ADC boss for filing. So this is my big in action before going to work with your file. Using your edit okay. This is what I want to execute their actual logic inaction logic. What I need is the lines which are matching with the root word. Those lines on the I want to print right. Then end what is end after completion of your actual logic. I want to simply print Complete dirty word action completed work on routine DC password file that's it then provide your input what all 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 all you are getting output because of this logic. But before this I want to print something then that you can write in your begin after completion of this I want to print something out only print. You can add some logic also. Well going forward you'll see that now you're getting that. So this is because of the end and this is because of start block I will begin blog. And this is your actual because of your actual logic. So this is your begin then you are getting this output and this is your actual logic and you're getting this output and this is your end the block and block logic. Because of that you are getting this. Okay so guys this is very simple abode you are in a black escaped right. So well going forward. Yeah we have one more thing right. Ed a black guy funny of this option. Let me show you that as well. See what I'm writing is so what was the logic you how here no inside off your single quotation right. Let me copy that my ED obligated or AWP a script so I'm going to copy and paste that logic here. Right. And I'm going to rearrange in a clear way so that is your big impact. This is my actual logic right. Then you how end and also write long enough. I mean indentations but it's better to provide but don't provide quotations no knowledge of quotations whenever if we are going to write your logic in a script. Now LW K I find if you're a W.K. then fine that's it. OK this is one of the way to write your edible case and how to execute that right. So they can also write by mentioning shebang line. We will see that line going forward. Right. So guys before going to write any and oblique escape we had to remember the standard syntax okay. Okay guys thank you for watching this video.