 Hi everyone, again, welcome to this lecture on the Linux programming and scripting. I hope you are enjoying the class so far last week we started with the full programming. This is when the course is getting serious in terms of the programming itself. So today we will be continuing the next this course on Linux programming focusing on the pull last week we introduced the basic commands actually we saw how to actually get data from the terminal or external input and then how we can actually manipulate that input and then produce an output which is like hello world program and it also gives the username that is what we saw and then we also saw the basic data types so the scale of data type the arrays and then the associative arrays today we will continue that looking at some more the data types one of the special types of array called string and then we look at that and then we will also like start some some of the operations using these data structures and how we can build programs so we will build it little by little so let us look at some of the strings so string is essentially like I mean it again an array of characters but together they have a special meaning which is they are treated as a single variable. So there is a concept of a matching which is essentially what it means is whether the two strings are exactly the same so like I mean in equality of numbers is fairly easy and say like 2 plus 3 equal to 5 and 5 on one hand and then 2 and 3 on the other hand if you do a addition operation they are equal that we can establish but in a string how can you establish whether a string is equal to another particular string so it is mainly what we call as the comparison or matching and essentially the matching is achieved through this double slashes as you can see here basically flash string slash there are a couple of mnemonics here one is from this this dollar underscore it actually has a special meaning this one here which shows here the dollar underscore refers to the current line so the current line or essentially current value basically as to where it is so here you can see basically dollar underscore is directly refers to what is a standard in essentially like I mean so again this particular statement you can see that it's a compound statement it basically gets this value into dollar underscore and then it also does the chop function on it one thing is basically the dollar underscore and that some of these variables you don't really need to specify actually like okay I am going to operate on that whole kind of implicitly assumes that if you don't specify anything you are referring to dollar underscore so that's again another reason why like I mean in this statement we don't specify dollar underscore equal to or something again pay attention to the two slashes and the string so in order to match this particular any string with the dollar underscore we just simply use the string in the middle of two slashes so here so the thing is basically if the result is true I mean then essentially like I mean that that path is taken if you are using like an if statement and again like I mean you can use this essentially like I mean if the string appears anywhere in that particular line in anywhere in this line again so the dollar underscore denotes the line and this particular string if it happens to be there anywhere within that line then the result is becomes true when you do the just the flash string flash so in this example actually so here people with the sierra academy dot com in as a URL essentially like I mean so you can specify like I mean then anything basically here ms students or electrical engineering students dot sierra academy dot com but as soon as finds out that sierra academy dot com is there in that particular as a sub string of this particular string then the result becomes yes so and then if you give any other one of course result is no and so the else back so this is a simple matching function so as we see basically like we are slowly becoming more and more now like okay we have some data structures how we can do programming with this data structure this will be the discussions that we will go forward with and then we will see like how we can manipulate the data itself using various commands so regular expression is the other thing that we will be talking about actually this is just introduction introduces the regular expression but we will go into more details later on so in general so we saw that basically like if it is a dollar underscore here then we use just the slash slash but in general like I mean the variable may not be just the dollar underscore you will be actually using other variables to do like string matching so in order to do those string matching essentially we use this till day tilde after the equal to and so equal to tilde means like okay now match and then we then build the string and then you can match again here you can see that actually I am also like putting a dollar at the very end this dollar indicates that that is the end of the line so or end of the line or end of the variable is where the dollar is look at the previous one actually here we do a chop or as well basically that chop actually chops anything white characters after the white characters between the non white space and the dollar variable you can think of it that way so in this case like I mean we are doing the chop again here on the dollar URL so first we are assigning the standard into the dollar URL that is the variable and then we perform a chop so it removes all the white space characters so all your left with is a non white space character followed by a dollar so that is why like now when we do a matching essentially which is now right here with this tilde that is the one that is used for matching now we can match it and then basically like this dollar essentially anchors that particular string at the end of the variable so it looks for this particular thing at the end of the variable so can anyone tell me like what is for the beginning of the variable so to go to the beginning or basically like match from the beginning of the variable we use a carrot the carrot is essentially in your keyboard you can look at as the shift six essentially that is the carrot so we use that for anchoring the beginning of the variable and the dollar is used to anchor the end of variable so essentially the regular expression itself we haven't really introduced in this section but now we will we will talk about the regular expression because the regular expression is one of the key things in Perl which enables it to actually be used by many many many script writers so here the dollar name is matched with John and can anyone tell me like where this John usually comes so as you can see basically since the character is used actually that the variable is anchored at the beginning so this John has to be the first four characters of the string in order to match now we follow it basically with a slash B or backslash B which is essentially like denotes a blank character or white space and in this one actually it's kind of different basically we now say that it's John and then slash I the I actually gives us essentially like I mean that is we don't have to match the exact case so uppercase John lowercase John both can become true in this case so so that that's another unique thing about well now here there is even longer thing basically so here if you notice actually there are there are two slashes or actually three slashes one year and then there is also this yes this is basically like I mean there we now start substituting the string with another string in this example the string HTTP is substituted with the other string the STP so essentially like I mean you can think of this as like I mean from the beginning basically like I mean if there is an HTTP then we replace that with the variable the string FTP and again this is so this particular command is in fact used very widely like I mean for that kind of string matching and then basically like strings so this is another example okay so as I was explaining so we can use like the various matching so a simple matching is from here there the string is matched against or the name is matched against the string John one thing that I want to highlight is here basically you can see that actually matched with the beginning at the beginning of the string so the problem name variable matched from the beginning so you can think of this as like it's anchoring point so it anchors either in the beginning or at the end the beginning anchor is this carrot and as we find the previous one the ending anchor or the towards the end how we do it is with the the dollar the dollars and so so this is a simple matching basically where it just is better this is the same or not so you can use this matching inside conditional statements to see whether we are getting the right value or not now the next one is with the backslash B again this is backslash B is a character that is used to match blank character so the John followed by blank is what is matched point the next one is flash I this is essentially like I mean as I mentioned like makes it like case-intensive in this case actually you know that even the J will the case of the J will also be small so this will match either like lowercase John like this it will also match J and then uppercase OHN whichever one if the cases it doesn't matter it just matches this set of characters doesn't have to be legato uppercase or low then the next one is essentially like this is where we are replacing the character the HTTP with FTP and whether it's uppercase HTTP low cases HTTP doesn't matter because of this I that is of the end basically then we do that directly and more let's look at this one this is also like a replacement can anyone tell me like what is being replaced so if you look at the difference between this and this we call it like TR and this side it's essentially it's S the S is essentially for a string replacement so the string replacement essentially may mean that like we just replace exactly the same string with understand in this case the TR stands for the transliteration transliteration operator and TR essentially changes the case in the in this example actually all the uppercase will be inverted into a lower case so so in the in so it takes the dollar name so it here actually like I mean if it is basically it takes the dollar name and there ever HTTP is found in this case all the characters are used in this translation and then in this way that translation works is it takes the uppercase once and then convert them so if there is already some lower case it leaves it as is but if there's any uppercase that it's become a low case so these are some of the examples of how we do string matching and the kind of we are beginning to now talk about regular expressions which is essentially like I mean a way to represent strings which then becomes easier or get being operated on so that's the way like I mean you can look at it essentially and the end character is also like not just so we we introduce this I there are other characters that we can use at the end for example we can use G so here instead of I you can use G these essentially matching globally so this probably matches only the the first occurrence but if you put a G then it matches all the occurrence of John in this particular variable and then we can also do string matching or matching against multiple lines which is by using M of the character and then there are few more characters which we will study in the future lectures but they are like a little bit more complicated and then couple of things that we haven't really understood yet is now we know exactly how to match the exact string and how to replay so some of the strings and also let some of the characters there are some shorthand locations for various these functions our these patterns for example the D represents or backslides D this will represent a digit basically which is like anything between one or actually zero and nine so a shorthand notation for this is the slash D there is one character and then if you add a class this becomes like one or more character if you put a star instead of a plus then that becomes zero or more of that characters so these are some of the nifty things that we can use for evaluating and then there is also like a upon the w which is essentially a backslash w is a letter essentially again which is from a to v so and then we also have there are two of them two different characters one is slash backslash s the lower case s and then the other one is backslash upper cases the lower case s signifies space character it is similar to like this backslash p which is essentially like having used for denoting white space between two words and then you can have multiple white space by just adding plus sign and then the upper case s is a non-white space character which could be the same thing basically nine or eight z then the upper case also possible and any other special character they're all included in the the upper case yes so with these kind of usage actually like now we can build I mean we can match exactly like some specific things inside this inside a spring or inside a line and then we can actually use that information in processing that data so I think like I mean this is all like probably it's a little bit more than what we wanted to look at the beginning of the lectures but I think like I mean it will be it will become more and more clear as we go along because these are the things that we will use to match and find on so now let's look at a function definition we actually use the function in the previous examples the chart function of the team we even though the definition of that is included as part of the first kind of library but the way the define is using this sub okay now let's look at the functions essentially so we actually saw a function previously which is the chart itself actually that's more like a library based function so it's like a standard function provided by the poll itself in general the function definition starts with the keyword sub so here you can see the sub and then the function name itself which is here it's in header we can have like some argument here there are no arguments there are arguments we will give that within the the parenthesis and then we start this bracket and then we just specify what exactly is the content of this particular function so here the content is actually like use print and then we just say basically what is the content I whether it's a text HTML and then followed by I think like this one you are familiar with now this is the new line character so it prints couple of new lines so essentially like I mean this is this may be a print header header information on an empty on an HTML page if you want to print something out then it this particular subroutine actually prints that thing and then prepares it for the subsequent content to be printed out and then the call is essentially we just say print header with either this the two parenthesis the parenthesis or simply this print header so we will be dealing with some of these these functions and function definitions and function calls in the later section because the program is itself is not fully done if you don't have these things by the way we already talked about it in the first class that in Perl there are no like there is no main routine which actually be executed first here basically just works from top down so the function calls could be anywhere and you can call that anytime basically so the other way to specify a function is to specify this particular command called require this is essentially like I mean that there are functions that are already predefined and kept in the library so when when we specify this require as a beginning at the beginning of the Perl script then this particular function will be incorporated within your program or it will be called so that whenever you use this the function call or one thing I forgot to mention in the previous one is when we call the whatever the function calls we also use this ampersand at the beginning ampersand in general you can think of it as like an address or whatever so the ampersand print header is essentially like I mean the thing how to call a particular function so here again for the C time we just follow with this ampersand C time and then here the argument with time and then it prints out one of the then the output essentially so this one you can you can actually like go to the Perl library and actually look for C time and see what it does so now let us come to how do we open a file the opening of a file is achieved to this command open and then there are two arguments to that here you can see that basically so this is straightforward which is the file name and what this is called is file handle you can think of this as the address of that particular file so file name the name itself doesn't mean anything but once we start so the name is associated with just this file handle and then after opening the file we always refer to it with its file handle so here essentially the file handle is using this log essentially and then basically like the greater than and less than so now can anyone tell me how do we read input from the terminal essentially as you you must have seen and basically as we learned in the very very beginning of the this course all the utilities all the resources in the in the unit system or the linear system is all represented by files so even the terminal is also a file and that file name is this stdin so that is why they are standardized defined well predefined file handles the stdin stdin out stdin error and then there is also like some arg once essentially arg v and arg c we will learn about that but think of it this way right the stdin stdin out they are all like files that gets written out or read in and then we use the same file handles to read various variables or read into these variables and basically write out so it is very similar to the same log and in this case actually we are just opening a custom file with a custom file handle whereas stdin stdin out stdin error they are all predefined and they are given to you already so a typical construct is this construct which is like while and then this and so in in this Perl program essentially like I mean want to open a file for every line so when we specify that while and then this greater than less than log this gets line by line so the first line will be there and then we can process that first line and then since this is a while loop it goes and processes the next time it reads one line at a time from this particular file so here the keeping is like I mean it just gets the access log and then basically it prints out access log and $ underscore here the $ underscore directly is equal to this log so it is typically you can think of this assignment is like this basically equal to so you can this is the same so while and then basically like this and then then do this same thing so the $ underscore is a special character as I mentioned earlier this is denoted to the current line or current variable whatever is there in the system there are similar $ variables they are called $ variables we will be actually learning some of them actually because they are also like very crucial and in fact you can manipulate things with these kind of using the $ variable for example how do we read a particular file so that so here like I mean one thing you notice exactly when we do this this while loop always it gets one line at a time how do we change or can we do something to change this to read multiple lines and use something else as a delimiter so delimiter is essentially like I mean that is how it reads essentially it knows that the end of line is basically the $ or essentially the end of line character so it reads up to the end of line so now say like I mean we want to read like any like multiple lines but on a semicolon the semicolon can come maybe we are maybe on a period basically we want to get one sentence at a time from a text file how do you do that and then these the sentences can be like spanning multiple lines the only delimiter on the sentences the period so think about that in fact that may be one of the exercises that we will ask you to do at some point there are ways to do it actually like to change the delimiter from the $ to something else and how do we do it we will talk about it so now for writing to a file essentially we use the redirect essentially which is the greater than so we open the file but if you use this one in front of the file name then it knows that okay this file handle like actually it is going to be a right file essentially so in the previous one this was like a read only file we are reading it from the access.log but now the right file is essentially from my find out text and then essentially whenever we use the whenever we want to write something to the file we also add the file handle to the command so if you do just print and then welcome so that does not print it into the file but if you do print with this file handle my file and then welcome now this welcome will go into this move on. So one question I have is if you do just print and welcome what does it do does it write to some place or how does pull react so the short answer is essentially as you know everything is a file so when you say like print basically without any argument it assumes that that is what you are asking it to do is to write it into the standard out so it uses the standard out as the file handle and then just writes out as the output and then finally we have this particular statement the close and then the close statement essentially like closes the file usually like when the Perl program finishes running the operating system goes and actually closes all the files that were open during the operation but it is a good idea to actually close the file yourself because sometimes you may be using like multiple files multiple times you may have to read and write the files so you will miss out as to whether you really close or not and for closing any file we have to give it the correct file handle here the my file is the file handle which we need to specify in order to close the file so we will come to some of the other interesting ways to manipulate the data later on so now let us look at the data types and basically like or how do we operate on some of this data so number one is this scalar data we already thought the scalar variable but now the data is essentially like these are it can be numbers or string of characters so any particular string is also a scalar string and one thing note is essentially like as I mentioned earlier Perl is not a strongly typed language and there are no integer values internal people all the numbers are stored as double position floating point numbers so again this is one key difference so that you can operate integer with floating point number and Perl will accept it and then produce the result there are some rules that you need to follow which I will explain as we go along and then the other one is basically the the literals or the constants are the way the data is represented in the source code so all the floating point little you have legal input so these are the things that you want to keep in mind so what are floating point with literal some of the examples are 1.234 1.23 e to the power 45 or minus 6.5 or minus 6.54 e to the power 3 or minus 12 e to the power 34 these are all floating point which is the integers are essentially like what is shown here sf could be an integer octal 177 is actually an integral value of 127 hexadecimal sf is 255 so for the string the examples basically there are no size limit can be from anywhere from 0 there are two different types of strings here in Perl one is the single quoted string and the double quoted string so the single code string that we already saw in the examples before essentially like I mean those are we used some of those things in those examples the double quoted strings are more common essentially so the difference here is like how much we can do the operations on in within a double so the key difference between in the double code string is that you can actually use variable names within the the double code string and it Perl can interpret that as a variable so that it replaces that with the actual variable during the execution or when you are running the program whereas in a single quoted string they are all just taken as a little string so essentially like even if you put a dollar something is the same as what it is so here essentially like I mentioned again the double quoted strings essentially they are strings like exactly like in C you can use also to you can use the backslash characters for escaping the control characters and also like I mean they are used as the control characters as well so you can specify like I mean backslash and that's that's one of the things that we saw in earlier work basically backslash and represent the new length character and then number seven in asking you represent the bell sound hexadecimal x7 actually like the f denotes that basically and then that actually no sorry x denotes the hexadecimal and then the 7f is the character that's for delete and another example is this variable interpolation in double quotes so and some of these things that we saw basically are new line character backslash and the return character or it's backslash r cx is essentially control x it is common emacs for editors backslash t is tab then the response speed some of these things are you hardly use them today but I think like I mean this is a complete list of all the characters that you can use how we can escape them so I will let you read some of these things at your leisure you can think of you can see basically even like the quotes themselves can be escaped and so it can print out the real ones so here are some examples a simple square string with double quotes just say hello which is prints hello now don't it prints just the don't with the single quote within then hello world basically it prints hello world with the new line character so it goes to the next line now a single quote the single quote is also represented as backslash quote the backslash itself is represented as the backslash slash and then the no other backslash characters are allowed essentially in inside the single quote strings so you can only like escape two things one is the backslash itself or the quote itself and then the string should not cross the line boundary and then there is no variable interpolated so if you are using like dollar something dollar sentence it just prints out as dollar sentence it won't change that to what the equivalent value is so the dollar variables are just represented as there is no interpolation and I think like I mean this is probably the biggest difference between a double quoted string and a single quoted string in a single quote there is no variable interpolation whereas in a double quote if you add a variable in the middle they are interpolated and expanded the variable the values are used when it is when you run the program so some of the examples like hello so this is exactly the same as double quoted hello so there is no difference there don't or don't actually we need to escape the quote otherwise the string will end at two on so if you escape it then it prints out properly and then the hello world multiple lines essentially like I mean so it prints out with the new line that is added here and then new line itself is basically it's just a two character spring so exactly the same so one question for you is what does this mean basically okay to maybe like more challenging what is this dollar a equal to quote we will put backslash backslash and the column and then dollar B equal to $20 B so try this program and then see what the next we will talk about it in the next class as well exactly that we will see so I think that's the end of today's class so we will see you in the next one so just to recap essentially we started talking about some string variables specifically string matching how do we match strings and then we also went through like how to do some of the the matching itself like I mean how do we operate on the strings we studied about little bit about the regular expression we haven't really introduced in the big way I will try to do that in the next class as to give you like more gory details about the regular expression and how can we do matching on really esoteric items and and you know like I mean wherever you can find some regularity you can be able to do the expression and then use a lot of matching and this this matching and this associative arrays are probably two main topics in Perl which is useful across where you are and which company you work for everywhere you go they will all be using these two principles the associative arrays and the regular expression matching so we saw that and then we also now started talking about the the strings as to what kind of string that we can build what is the difference between a single quote and the double quote and the key difference there is the double quote will do the variable enumeration or actually the it will extrapolate all the variables whereas for a single quote it won't do those the variable interpolation so I guess that is pretty much currently like I mean what we have and then we will start talking about how we go about in the next class okay thank you very much