nawk Associative Arrays HELP!
I am trying to create an associative array "table", where the key of
an element consists of a line from the "usernames" file, and the value
of all elements is 0. I am suppose to use this code snippet but I don't
have a clue of how to go about it.
So far I have created the following awk file asg6.awk which contains the following code: #!/bin/ksh
if($1 in table)
I enter the following command at the command line but the system just pauses and nothing happens: nawk -f asg6.awk Can someone please help me out. I am very new to nawk and totally lost. Thanks. -------------------------------------------------------- To be more specific on the layout of what my awkfile should contain,
here is the format of what my file should look like:
{
END {
Also, do I need to add anything else to my file in order for it to execute properly before the "BEGIN" statement? Any help would be appreciated. Thanks. -------------------------------------------------------- Your obvious effort on this assignment deserves some assistance.
Within awk, shell variables must be handled differently. ?A $ is used to represent a field (0-199). ?Example, assuming the current line is "the quick brown fox": print $3 ????# prints brown
For the last line above, there is a double interpretation: First, fnum is evaluated as 3, then $3 is evaluated as brown. Shell variables can be passed when awk is invoked with -v option. In the script below, I chose to pull it from the environment with ENVIRON function. ?There are couple other ways also. Your script has only a BEGIN and an END statement. ?Notice that the layout in your follow-up posting shows: a BEGIN statement to create the array
The BEGIN statement creates an array, and the main and END statements need to reference that array by using the same name. That second (main) statement is the code used to process each line in tina.txt. The awk command needs to name one or more files, such as: ??awk 'your program' tina.txt As your specs mentioned, the key of the array is each entire line from usernames (as read into var). ?If any of these lines have more than one field, they can never be matched by field #1 ($1) from tina.txt. #!/bin/ksh
{if ($1 in table)
END {
-------------------------------------------------------- Thank you so much for responding. I think I've got it now. I've been working on this assignment all weekend and you're input was most helpful. I am moving on to perl now in my assignment (yippee). No, actually, I enjoy programming -- it's just that this class has been jumping around pretty quickly as of late. Anyway, again -- thank you for your help... :)
Have a Unix Problem
Unix Books :-
Return to : - Unix System Administration Hints and Tips (c) www.gotothings.com All material on this site is Copyright.
|