PHYCS 3730/6720 Lab Exercise


Exercise 1.

Create a two-column data file, called file.dat with x-y coordinates of several points (using emacs or cat). With awk, find the product x*y of for each line in your datafile:

awk '{print $1*$2}' file.dat
Explore some of the possible numerical operations of awk by trying to use sin, cos, exp, log, functions (e.g., cos($1+$2)).

The single quotes around the first argument are needed so that the shell does not change anything before it passes the argument string to awk.

Awk's second argument, the filename, tells awk where to get standard input. If the second argument is not present, awk expects input from the keyboard. Use awk with only one argument to find the product 0.12345679*9. Try this by using the "$1*$2" form above; you'll have to type in the numbers, separated by a space, then hit C-d (control-d) to signal to awk that you are done giving input. Then open the exercise answer file Mylab02.txt in an emacs window, type "Exercise 1" and copy and paste this last awk session (the command line, your typing, and the result) from your terminal window into the exercise answer file.

You can use awk as a simple calculator as follows:

awk 'BEGIN{print sin(4*3.14159265/3);}'

With the BEGIN in this case, awk is not expected to process any input. It just evaluates the expression and quits. Try it.

Exercise 2.

Read the discussion of and Copy the file from my directory ~p6720/exercises/awk_and_simple_python. Run it. Then follow the suggestions in the notes to make the Python program fail in order to familiarize yourself with some of the common error messages. Nothing to hand in.

Exercise 3.

Modify the program so it uses the eval(input()) function instead of the plain input() function, as explained in the notes. Also have it print your age in five years.

When you are satisfied with your result, copy your modified code to the exercise answer file under the header "Exercise 3" and submit it.