PHYCS 6730 Lab Exercise: Command Line Arguments and awk example

The wavelet.cc code, found in ~p6730/examples/a06 accepts command line arguments. In the homework for this week, we use awk scripts to do wavelet filtering. These exercises show how command line arguments are processed in C and C++ and give some practice with the awk utility.

Exercise 1 Reading command line arguments

1. Please start with the executable ~p6730/exercises/command_args/read_args and its source code read_args.cc. This program simply lists the arguments that you put on the command line. Compile and run the code with as many command line arguments as you like. (Nothing to hand in.)

All the arguments are character arrays. The address argv[i] points to the character array for the ith argument. The functions atoi and atof convert a character array to an integer and a double, respectively. The system header file stdlib.h defines these functions. Use the man pages to see how to use them.

2. Then modify the code so it looks at only three arguments (counting the command itself) with the second argument, an integer and the third, a double. Do the conversions to numeric types and write the results. Put your modified code in your answer file.

3. In this exercise you are asked to compare two strings. The wavelet.cc code uses strcmp to see if an argument string matches a desired pattern. Here we use the standard library string class. The header string declares the string class. Modify your code and call the new version read_args2.cc. Have your new code take four arguments (counting the command itself), compare the second argument with the string eureka and report the result of the comparison. Put your new modifications in the answer file.

Hint: You'll need to construct a string object from the character array that argv[1] points to. To declare the object a, and set its initial value to the character array argv[1], use the declaration string a(argv[1]);. You can build relational expressions with string objects, using operators such as ==, >, and <. And you can use string constants, such as "eureka" in expressions with string objects.


Exercise 2. Simple awk scripts

Please see the introduction to awk in the Computer Lab Manual .

1. In the file ~p6730/examples/a06/pion there is a table with six values per line. Write an awk command to display only the first three numbers in each line. Put the command in the answer file.

2. Let's call the first three numbers kappa, mpi, and dmpi. Write an awk command to generate a table with three columns, the first being 1/kappa, the second, mpi2 and the third, 2*mpi*dmpi. Put the command in the answer file.

3. Put the awk command in the previous problem into a script file called pion.awk and run the command using awk -f. Copy the command and its output to the answer file.