#Contents of lesson 4: #To pass a variable to an awk command on the command line, make sure that we enclose the variable in a pair of single quotes. ex: %name=$USER #Set the variable "name" to be the user's login name %echo $name #Should see the display of the user's login name. %who | awk '{if($1 ~ /'$name'/) print}' #will get the record contains the user's login name. #To pass a variable that contains more than one word to an awk command on the command line, make sure that we enclose the variable in a pair of double quotes, then enclose everythng in a pair of single quotes. ex: %name="Jen Chen" #Set the variable "name" to be the string "Jen Chen" %echo $name #Should see the display Jen Chen #Make sure that you have the manyOrders.ksh file so that you can see the result of this script. Note that we scpecify the field delimiter to be a colon (:) since the 1st line in the manyOrders.ksh script contains the line "Author: Jen Chen". %awk -F: '{if($2 ~ /'"$name"'/) print }' manyOrders.ksh #This will print the first line frm the manyOrders.ksh file. #Note: All awk scripts start with the line: #!/usr/bin/awk -f Do not forget the -f option. Your script will not run without this option. Note also that the path to the awk varies depends on the OS you use. Thus it is a good idea to check out the path before you include that line in your awk script by issuing the command "%whereis awk". - An awk script contains three parts: BEGIN {........} {...................} #This is the body of the awk script. END{.......} - We can omit the BEGIN and END parts but not the body. - Usually the specify the field delimiter in the BEGIN{} part and other title that we want to print in this portion. Since whatever is in the BEGIN part will be executed only ONCE. - Most of the other commands are placed in the body of the script. - If we just want to print out the result then place the print statement inside the END{} portion. Again everything inside this part will be exeuted once. - As you see the main portion of the awk script lies in the body of the script. - The reason is that awk operates ONE record at a time, until it meets the EOF (end of file) signal. Therefore, we should pay attention to how do we want to work with each of the field in each record. All the fields manipulation are placed in the body of the script. The result will be placed, if we want, in the END{} portion of the script. Normally we would combine an awk script with a shell script to generate the result that we want. Since awk will normally require more energy to serve (since it works on records) therefore we should use it sparingly. We can write a long awk command at the command line, just press ENTER at the end of the page and make sure that your entire string is no more than 255 characters. ex: who | awk '{if($1 ~/jchen13/) print; #press ENTER else if($1 ~/ceduc02/) print; #press ENTER else print "No match"}' #press ENTER Of course then it would be more convenient to write this in an awk script then call that awk script on the command line, or run it by itself. -Assume that we have an awk script named "manyOrders.awk" and we would like to run this on the command line we then issue this command: %awk -f manyOrders.awk Order_Details.csv #Note that this is not the intention of my manyOrders.awk script. It has to be used with the manyOrders.ksh scrit in order to generate the correct result. ............. (..........Will be continued)