Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Unable to understand associative nature of awk arrays Post 302091764 by tayyabq8 on Thursday 5th of October 2006 04:51:39 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
esdrec(1)							   User Commands							 esdrec(1)

NAME
esdrec - record a sound file SYNOPSIS
esdrec [-s hostname:port] [-b] [-h] [-m] [-r freq] DESCRIPTION
The esdrec application pipes data from the sound device to stdout using the Enlightened Sound Daemon (esd). OPTIONS
The following options are supported: -s hostname:portContact the esd server on hostname at port. -b Specify that the output should be in 8-bit format. By default, the output is in 16-bit format. -h Display help text, and exit. -m Specify that the output should be in mono format. By default, the output is in stereo format. -r freq Specify that the output rate should be set to freq. By default, the output rate is set to 44.1 kHz. EXAMPLES
Example 1: Record sound from sound device to file "sample" example% esdrec > sample EXIT STATUS
The following exit values are returned: 0 Application exited successfully >0 Application exited with failure FILES
The following files are used by this application: /usr/bin/esdrec Executable for Enlightened Sound Daemon sound recorder. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWgnome-audio | +-----------------------------+-----------------------------+ |Interface stability |External | +-----------------------------+-----------------------------+ SEE ALSO
esdcat(1), esdmon(1), esdplay(1), esdsample(1), esdctl(1), esd-config(1), libesd(3) NOTES
Written by Brian Cameron, Sun Microsystems Inc., 2003. SunOS 5.10 8 Jan 2003 esdrec(1)
All times are GMT -4. The time now is 10:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy