IT244: Introduction to Linux/UNIX – Fall 2010 Project 2

advertisement
IT244: Introduction to Linux/UNIX – Fall 2010
Project 2
Due: Wednesday, 12/01/2010
Project Description
You will create your own awk script that will act as described below…
 Given the command “ps auxw | awk –f awkscript”, you must create
a file called awkscript that will do the following:
o First, read up on awk’s printf command and, in the BEGIN
block, print out a formatted header line with the following
headers. The only real formatting is that the headers need
to be printed in tabbed position above the correct column of
data:
 Username
 Process ID
 Ranking
o Create an array of lowest and highest PID values for each
user. The array will be keyed on “<username>:lowest” and
“<username>:highest”. The value for each key will be the
lowest PID found so far.
o In the END block, they should be able to use printf to print
out a formatted report underneath the headers that the
begin block printed. The report should look like the
following:
Username
Process ID
Ranking
bluemoon1
125355
Lowest
bluemoon1
125357
Highest
bond008
1335
Lowest
bond008
125535
Highest
…
 Don’t worry about whether or not the usernames are printed
consecutively. All I want to see is that you gathered the data
correctly.
Important Information
1
Use the following information as a resource for completing the project
assignment:
 Remember the difference between the three sections of an awk
script. There is the BEGIN section, which happens once, and is
associated with one curly-brace block of code, otherwise known
as an action. There is the END section, which also happens once,
and is associated with one curly-brace block of code. There is also
what comes in the middle, which can be any number of
pattern/action pairings. You only need one action for this awk
script with no need for a pattern.
 The printf command is not that difficult. Just look at the example
on page 548, and the syntax on page 538. On page 548, the printf
command line says:
o %-10s , which means %s, which is a string variable, and 10,
which is the minimum width in characters for this field, and
– which causes the column to be left-justified. Without the -,
the column would be right-justified. The whole format
string, including spaces and tabs, is surrounded by doublequotes to make sure that spaces and tabs are kept intact,
and followed by a comma, and then followed by a commaseparated list of variables. This list must match one to one
with each of the % definitions in the format string. The first
variable will be substituted in place for the first % definition
(in the given example that would be $1 being expanded in
place of the text “%-10s”). And subsequent variables will be
substituted for subsequent % definitions in the order they
are found.
 Arrays (or more correctly, hash maps) are variables where the
name is followed by square brackets, and within the square
brackets is the string to be used as a key. If the string is a hardcoded string, you would surround it with quotes. That’s not what
we need. We need to put the content of a variable in as the key.
Then you can assign that the value you want. You will then
retrieve it by preceding the same variable with a $. Here is an
example of setting a value using the content of a variable as key,
and retrieving a value using the same key:
o myarray[$myvariable] = “some value”
o newvariable = $myarray[$myvariable]
2
Download