September 7, 2004 --- Class 3 --- Redirection, Axis, AWK, Intro to Euler Method Activities: Filters, Pipelines and Redirection UNIX has some wonderful facilities that make it easy to combine simple commands together to do complex jobs. The key ideas are "standard input", "standard output", and pipelines. Many commands require input to do their work and are set up to use the "standard input", which is normally the keyboard. Suppose you have prepared a file with the input you want because you know there are a lot of commands and you may make a typo. In UNIX do this: command < infile This runs "command", but tells it to "redirect" the input from the keyboard to the file named infile. So, the infile is used as the input and you don't have to type all the input commands. If the file is setup, so that its output goes to the "standard output", you will see the output at the screen. But, what is good for the input, is good for the output, so if we type command < infile > outfile then what would go to the screen instead is redirected to the file outfile. Any program that takes its input from the standard input and sends its output to the standard output is called a filter. Think of a red filter. On the input side is any kind of light. On the output is only the red component of the incoming light. With filters, you can then pass the light through another filter further modify the light. In UNIX, a filter takes information from the standard input, modifies it and sends it to the standard output. But how do we send the output of one filter to the input of another? We do that with a pipeline. filter1 out2 takes input from in1 and runs command filter1, then it takes the output of filter1 and uses is as the input to command filter2. Finally, it puts the results in file out2. We will have many opportunities to use this capability. axis axis is a simple graphics program that can make publication quality graphs. You may wish to use it for this class and your other work. There is a color version of axis. You should use /usr/local/bin/axis and /usr/local/bin/plot The following potential problem has not arisen recently, so if /usr/local/bin/axis data.ps lpr data.ps The axis handout has many more details of how to make a nice graph. AWK If you have \delta t values in a file and you want to have \delta t and 0.5*(\delta t)^2 on each line, it is easy to do this with an awk command. Assume the \delta t are in a file with one value per line and the file is called col. awk '{print $1,0.5*$1*$1}' col will result in two columns with the desired numbers. The output may be redirected to a file if desired. awk may be used to create a list of numbers, as well awk 'END{for(i=1;i<$1; ++i) print 0.01*i}' can be used and you may interactively type a number and then on the next line ^D (which stands for pressing d, while holding down the control key). AWK is a very versatile pattern matching utility that can be used to manipulate files. Each AWK commands consists or a pattern followed by an action. If the pattern is matched, the action is performed. The action is placed inside curly braces. A very simple awk command is: awk '{print}' file_name The single quotes protect the curly braces from being interpreted by the shell as special characters. In this AWK command, there is no pattern to match, so the print command is always invoked. awk '/1/ {print}' file_name has a pattern that matches any line containing the character 1. Only such lines will be printed. awk '/^1/ {print}' file_name In this example, "^" is a special character to indicate the beginning of a line. Thus, only lines that begin with "1" will be printed. awk '{print $2}' file_name will result in the second word of each line being printed. (Words are separated by white space, i.e., spaces or tabs.) awk '{print $2,$1}' file_name prints the second word followed by the first word. You might like to try to the above examples with the file cols that you are welcome to copy from my home directory. Tea or coffee cooling problem (CSM, Sec. 2.1) This is the problem discussed in chapter 2. Should you add the milk to the tea as soon as it is poured, or just before you want to drink it in order to cool it as quickly as possible? To solve this problem, we are going to learn to integrate a differential equation numerically via the Euler method. A simple integral first (CSM, Secs. 2.2-2.5) In ~sg/chap2 you will find a file euler.c that you can use to do a numerical integral. It might be set up with the function 2*x or 3*x^2. To create an executable either type: make euler.c or cc -o euler euler.c I looked at two step sizes 0.1 and 0.05. The numerical output is more accurate for smaller step size. In the next class, we will: 1) Make a graph of the result as a function of x. 2) Try different step sizes and see that the answer varies, but converges as the step size decreases. 3) Create (understand) the script ~sg/chap2/ans_vs_step.csh 4) Plot the result as a function of step size. It would be useful if you read Chapter 2 before the next class. You don't have to solve the problems! (Yet) Some will be assigned for homework.