Unable to understand associative nature of awk arrays


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to understand associative nature of awk arrays
# 1  
Old 10-05-2006
Unable to understand associative nature of awk arrays

About associative nature of awk arrays i'm still confused, not able to understand yet how array element can be accessed based on a string, I got one example at gawk manual to illustrate associative nature of awk arrays, it goes here:
Code
Code:
awk '
# Print list of word frequencies
{
    for (i = 1; i <= NF; i++)
        freq[$i]++
}

END {
    for (word in freq)
        printf "%s\t%d\n", word, freq[word]
}' test.txtIf

this is the input file:
Code:
Kuwait  UAE     Qatar   KSA     Jordan
Lebanon Egypt   Syria   Kuwait  Qatar

Here is the output:
Code:
Syria   1
KSA     1
Lebanon 1
UAE     1
Qatar   2
Egypt   1
Jordan  1
Kuwait  2

Can you pls explain the contents of freq at each iteration of first rule and also what happens at each iteration of END rule? It would be a great favor.

Thanks in advance.

Cheers,
An AWK student
# 2  
Old 10-05-2006
i want to explain, but my english is not good enough.
maybe this could help.
# 3  
Old 10-05-2006
I have modified your code to illustrate the contents of freq at each iteration:
Code:
$cat freq.awk
#! /bin/ksh
awk '
# Print list of word frequencies
{
    for (i = 1; i <= NF; i++)
{
# Here loop variable i will contain values like 1, 2, 3 until last field of 
# Record which in your case is 5, so $i will behave like $1, $2, $3
# Following condition will lookup in array if key($i ie name of city in your case)
# Already exists in array or not

        if ( $i in freq )     
        print "Key(" $i "): is already there in array and its frequency or value is:  " freq[$i]

# In the 1st iteration of loop, $i($1) is equal to Kuwait, so freq[$i]
# Means freq[Kuwait], because you dont have any value associated to key Kuwait
# Therefor freq[$i] yeilds to zero and ++ operator increases it to 1

        freq[$i]++

# Print the record no., field no. value of key and freq associated to key, at each iteration of loop

        print "Record No." NR "\tField No: " i "\tValue of key: " $i "\tFrequency of word: " freq[$i]
}
print "\n"
}

END {
# Loop thru the whole array(freq) and find the associated value to each key
# Here word denotes key and freq[word] refres value associated to each key

    for (word in freq)
        printf "%s\t%d\n", word, freq[word]
}' abc.txt

Your inputfile:
Code:
$cat abc.txt
Kuwait  UAE     Qatar   KSA     Jordan
Lebanon Egypt   Syria   Kuwait  Qatar

Sample Output:
Code:
$./freq.awk
Record No.1     Field No: 1     Value of key: Kuwait    Frequency of word: 1
Record No.1     Field No: 2     Value of key: UAE       Frequency of word: 1
Record No.1     Field No: 3     Value of key: Qatar     Frequency of word: 1
Record No.1     Field No: 4     Value of key: KSA       Frequency of word: 1
Record No.1     Field No: 5     Value of key: Jordan    Frequency of word: 1


Record No.2     Field No: 1     Value of key: Lebanon   Frequency of word: 1
Record No.2     Field No: 2     Value of key: Egypt     Frequency of word: 1
Record No.2     Field No: 3     Value of key: Syria     Frequency of word: 1
Key(Kuwait): is already there in array and its frequency or value is:  1
Record No.2     Field No: 4     Value of key: Kuwait    Frequency of word: 2
Key(Qatar): is already there in array and its frequency or value is:  1
Record No.2     Field No: 5     Value of key: Qatar     Frequency of word: 2


Syria   1
KSA     1
Lebanon 1
UAE     1
Qatar   2
Egypt   1
Jordan  1
Kuwait  2

Regards,
Tayyab
# 4  
Old 10-07-2006
Shereenmotor, Thanks a lot for your detailed reply.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Associative arrays awk

Hi, I have the following dataset. A 2 1 272 A 2 2 333 A 2 3 222 A 3 1 222 A 3 2 11 B 1 1 112 B 1 2 998 B 2 1 667 C 1 1 887 C 1 2 887 C 2 1 998 I need to have an associate array based on the first column and generate a auto generated number column in the last column. Needed output:... (2 Replies)
Discussion started by: mitt
2 Replies

2. UNIX for Dummies Questions & Answers

Unable to understand awk script.

Hi below is the samll awk script which i am not able to understand properly. here a bit confusion in 2nd line for me 1. why *\ is not used before . in second line as it was used in first line *\$*\. */ 2. why always \ is used in every first line 3. is there any specific meaning... (9 Replies)
Discussion started by: scriptor
9 Replies

3. Solaris

Cant understand the File system working nature

Hi, We have a filesystem whose usage has gone above 81%, i tried to zip some files in the sub directories but the space is not released in the main file system. > df -h Filesystem size used avail capacity Mounted on /u01/app 5.8G 4.6G 1.2G 81% ... (2 Replies)
Discussion started by: nokiak810
2 Replies

4. Shell Programming and Scripting

Improving code by using associative arrays

I have the following code, and I am changing it to #!/bin/bash hasArgumentCModInfile=0 hasArgumentSrcsInfile=0 hasArgumentRcvsInfile=0 OLDIFS="$IFS" IFS="|=" # IFS controls splitting. Split on "|" and "=", not whitespace. set -- $* # Set the positional... (3 Replies)
Discussion started by: kristinu
3 Replies

5. UNIX for Dummies Questions & Answers

Using associative arrays with an if statement

I have this piece of code. The first if statement is not working, however the second if statement is working fine. I have set a value for Srcs to be file.srcs and want to print it. If no value for Rcvs is set, I get the print statement correctly hasValue="file.srcs" if ${hasValue}; then ... (0 Replies)
Discussion started by: kristinu
0 Replies

6. UNIX for Dummies Questions & Answers

Unable to understand ps output.

I m executing ps command and sorting it according to memory usage. Please find the output of the command. # ps aux --sort pmem USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 2060 624 ? Ss 01:54 0:00 init root 2 0.0... (1 Reply)
Discussion started by: pinga123
1 Replies

7. Shell Programming and Scripting

associative arrays?

Hello, i'm writing a little script that checks a .txt file for a specific ID that came after 9:10 am which outputs it's data to a file LateUsers.txt once done , it should mention the following: Number of late users Number of unique late users Over all late users percentage number of... (0 Replies)
Discussion started by: rollyah
0 Replies

8. Shell Programming and Scripting

Associative arrays

Hi all, #!/usr/dt/bin/dtksh typeset -A wavelength wavelength=650 wavelength=590 wavelength=510 wavelength=475 wavelength=445 wavelength=400 I have created an associative array like the one above. Now I am trying to print the values If i give print ${wavelength} it is... (4 Replies)
Discussion started by: prasperl
4 Replies

9. UNIX for Dummies Questions & Answers

unable to understand a awk code

i am not able to understand the following code for awk: $awk -F"|" '{ kount++} >END { for (desig in kount) > print desig,kount }' emp.list the input file i.e. emp.list is :: 3432| p.k.agrwal |g.m |sales 4566|g.l.sharma |director|production 3433|r shah | g.m | production... (1 Reply)
Discussion started by: streetfi8er
1 Replies

10. UNIX for Dummies Questions & Answers

are Associative Arrays possible in UNIX?

Is it possible to say.. myArr=34 myArr=15 ? (11 Replies)
Discussion started by: yongho
11 Replies
Login or Register to Ask a Question