- OPS435 -

OPEN SYSTEM AUTOMATION

Lab #9

FOCUS: awk utility - Basic

PART A - Perform the following tasks:

  1. Logon to your Matrix account.
  2. Perform the week9 and week10 online tutorials called "Online Linux Tutorials". This tutorial must be completed to count as half of the mark for this lab #9. The tutorial, when properly completed will send your instructor and yourself an e-mail message to verify that the tutorial was completed.

    Here is a link to the online tutorials (instructions): [ html ]

    [ html ]

  3. Proceed to Part B
  4. PART B - Perform the following tasks:

    1. Logon to your Matrix account. This lab requires that you use your Matrix account in order to submit your lab.
    2. Make an empty directory, called lab09.
    3. Use the cd command to make lab09 your present working directory.
    4. Copy and paste the contents of the link below into a file called cable_listing.txt (in your lab09 directory).

      Here is the Link: cable_listing.txt

    5. Use a text editor to create a file called lab09.bash and add a shebang line to make it a portable Bash shell script.
    6. Add the following lines displayed below to your existing shell script:


      if [ $# -ne 1 ]
      then
      echo "USAGE: $0 [cable_listing.txt]" >&2
      exit 1
      fi

      if [ ! -f $1 -a $1 != "cable_listing.txt" ]
      then
      echo "USAGE: $0 [cable_listing.txt]" >&2
      exit 1
      fi

      echo "Channels for CTV:"
      awk -F"," '$2 ~ /[cC][tT][vV]/ {print $0}' $1


    7. Save and exit your file called lab09.bash, set execute permissions for your newly-created file lab09.bash.
    8. Run your shell script by issuing the following:

      ./lab09.bash cable_listing.txt

    9. Take a moment to notice what your shell script did...
    10. Change the awk command already in your shell script to read:


      awk -F"," '$2 ~ /[cC][tT][vV]/ {print $1,$2}' $1


    11. Run your shell script (as in step #8), and note any differences.
    12. Again, change the awk command already in your shell to read:

      awk -F"," '$2 ~ /[cC][tT][vV]/ {print $2,$1}' $1


    13. Save your work and run your shell script with the same argument as in step #8, and notice the results.

    14. You should see that the standard output (stdout) from running your shell script is not formatted well. The awk command can be used to produce formatted reports. The print or printf commands can be used with the awk command.

      The printf command stands for "print formatted" and can be used to align text, and use variables to insert values into specified areas in your report using format specifiers. If you have learned C programming, you will already know how to use the printf command...

      We will now modify your shell script to improve the output by using additional features in awk and by using the print and printf command...


    15. Delete the "echo" command prior to your awk command.
    16. Change the awk command in your shell script to read (this is one large awk command):

      awk -F"," 'BEGIN {printf "\n[[ Channels for CTV ]]\n\nStation\t\tChannel\n----------\t-------\n\n"} $2 ~ /[cC][tT][vV]/ {printf "%s\t%s\n",$2,$1} END {printf "\n\n"}' $1


    17. Save your changes and run your shell script as shown in step #8. You should note that one single awk command did all of this report generation including:

      1. Reading fields from a database
      2. Creating a title and column heading
      3. Creating the report detail (i.e. channel info and number) for pattern "CTV"
      4. Creating the end of the report (in this case two new lines).


      We can use the "printf" command (with format specifiers) to make it easier to align the output.
      Refer to my OPS435 course notes (week #9) to learn how to use printf command... We will also be showing how to include running totals...


    18. Replace your awk command with the following large single awk command below:

      awk -F"," 'BEGIN {printf "\n[[ Channels for CTV ]]\n\n%-13s%7s\n%-13s%7s\n\n","Station","Channel","-----------","-------"} $2 ~ /[cC][tT][vV]/ {total=total+1;printf "%-13s%7s\n",$2,$1} END {printf "\n\n%20s\nMATCHES%13i\n%20s\n\n","-------",total,"======="}' $1

    19. Save your changes, and run your shell script. You should notice that the channel column heading and the channel numbers line up in the right-side...

    20. Proceed to Part C

    PART C - Write a shell script
    1. Make certain that you are currently located in the lab09 directory
    2. Copy your lab09.bash file to a file called search.bash
    3. Modify your search.bash shell script to accept two arguments. The first argument will be the name of a database (which could be any file including channel_listing.txt). The second argument will be a pattern to search in the second field of the database. For simplicity, you don't need to worry about ignoring case sensitivity...

      EXTREMELY IMPORTANT HINT:

      You can display the value of variables in an awk expression (i.e. between the single quotes '')
      by adding additional single quotes around the variable.

      For example:

      awk '$2 ~ /pattern/ {print $2,$1}' input-file

      var=pattern awk '$2 ~ /'$var'/ {print $2,$1}' input-file

      below is a sample run using the database file "cable_listing.txt":

      SAMPLE RUN

      ./search.bash
      USAGE: ./lab09.bash [cable_listing.txt][pattern]
      ./search.bash moo CTV
      Error: File pathname "moo" is invalid
      USAGE: ./lab09.bash [cable_listing.txt][pattern]
      ./search.bash cable_listing.txt CTV

      [[ Channels for CTV ]]

      Station Channel
      ----------- -------

      CTV-Local 7
      CTV Newsnet 17
      -------
      MATCHES 2
      =======

      ./search.bash cable_listing.txt CBC

      [[ Channels for CBC ]]

      Station Channel
      ----------- -------

      CBC-Local 8
      CBC Newsworld 26
      -------
      MATCHES 2
      =======

      ./search.bash cable_listing.txt Global

      [[ Channels for Global ]]

      Station Channel
      ----------- -------

      Global- Local 3
      -------
      MATCHES 1
      =======


    4. Save your changes, and run your shell script to make certain it works...
    5. Proceed to Part D to submit your lab...

    PART D - Submit your Lab #9:
    1. Make certain that you are currently located in the lab09 directory
    2. Issue the following Bash Shell script to check and if correct, send e-mail to your OPS435 professor (works only for Murray Saul's sections):

      /home/murray.saul/labs/submit-lab9.bash

      This shell script should either indicate problems and hints for you to correct your shell script, or indicate that your lab #9 submission was successful. If your lab was successfully submitted, both you and your OPS435 professor (Murray Saul) should have received an e-mail message.

      Note: Keep this e-mail message for the remainder of the semester as date and time stamp proof that you submitted the lab in case there is a discrepancy with your OPS435 lab grade...