awk counting question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk counting question
# 1  
Old 06-14-2013
awk counting question

Probably a simple to this, but unsure how to do it. I would prefer an AWK solution. Below is the data set.
Code:
    1   2  3
    2   5  7
    4  6   9
    1  5   4
    8  5   7
    1  1 10
   15 3 12
     3 7  9
    9 8 10
    4 5  2
  9 1 10
  4 7 9
  7 12 6
  9 13 8

For the second column, I want to count how many times each value occurs and produce the following output which contains the number in the second column, and how many times it occurs.
Code:
1    2
2    1
3    1
4    0
5    4
6    1
7    2
8    1
9    0
10    0
11    0
12    1
13    1

Thanks!

---------- Post updated at 09:54 AM ---------- Previous update was at 09:06 AM ----------

I have tried the following code
Code:
nawk '{a[$2]++} END {for (i=1; i<=13;i++) print i, a[i]}' file

and have gotten the results
Code:
1 2
2 1
3 1
4
5 4
6 1
7 2
8 1
9
10
11
12 1
13 1

I need to include the zeros as well

Last edited by vbe; 06-14-2013 at 11:57 AM.. Reason: Please use code tags
# 2  
Old 06-14-2013
You can do that by "casting" the output as a number:
Code:
$ awk '{a[$2]++} END {for (i=1; i<=13;i++) print i, a[i]*1}' file
1 2
2 1
3 1
4 0
5 4
6 1
7 2
8 1
9 0
10 0
11 0
12 1
13 1

# 3  
Old 06-14-2013
I like +0 over *1, cheaper? I am a bit surprised *1 is 0, not having run into the unary * operator before. Well, it is awk! Smilie
# 4  
Old 06-14-2013
Quote:
Originally Posted by DGPickett
I like +0 over *1, cheaper? I am a bit surprised *1 is 0, not having run into the unary * operator before. Well, it is awk! Smilie
It's not a unary operator. It's the usual binary muliplication operator. Any math operation casts its operands to numeric type, so when a[i] is undefined, a[i] * 1 becomes 0 * 1, whose result is the numeric 0 which the print statement will convert to the string "0". Without the explicit cast, the print string context converts an undefined a[i] to "", the empty string.

Regards,
Alister

Last edited by alister; 06-14-2013 at 12:49 PM..
# 5  
Old 06-14-2013
OK, how many contexts say undefined or ' ' * 1 = 0, compared to ' ' + 0 = 0 ? Just sayin', works but not intuitive, and so less maintainable !
# 6  
Old 06-14-2013
I agree that "+0" is more "intuitive", but in this context either is equally maintainable. I don't use many untyped languages - typically only AWK and Shell, and have used +0 and *0 interchangeably in the past. In shell, the unintuitive also works:
Code:
$ X= 
$ echo $((X*1))
0

# 7  
Old 06-14-2013
Not always:
Code:
$ echo $((X*1))
ksh: X*1: bad number
$ dtksh -c 'echo $((X*1))'
dtksh -c 'echo $((X*1))'
0
$ bash -c 'echo $((X*1))' 
bash -c 'echo $((X*1))'
0
$

Bash and ksh93/dtksh but not old ksh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Word-counting and substitution with awk

Hi!! I am trying to write a program which allows me to count how many times I used the same word in a text: {$0 = tolower ($0) gsub (/_]/, "", $0) for (i = 1; i <= NF; i++) freq++ } END { for (word in freq) printf "%s\t%d\n", word, freq It seems work but... (3 Replies)
Discussion started by: ettore8888
3 Replies

2. Shell Programming and Scripting

Quick Question: counting columns?

Hi everyone I have a quick question... I have an ascii file that looks like the one below, and I wanted to find a way to make a listing of how many columns there is in each row, similar to the example below. Anyone has any ideas of how I can do this? Thanks!! 6 Sep 2008 -158.535 33.6617... (2 Replies)
Discussion started by: lucshi09
2 Replies

3. UNIX for Dummies Questions & Answers

Loop Counting Question

I figured this out, but I wanted to pose the question. I was writing a while loop that required counting the amount of times the job had run, for example. An example below is what I had: variable=0 while : do variable = `expr $variable + 1` done That didn't do what I... (4 Replies)
Discussion started by: phunk
4 Replies

4. Shell Programming and Scripting

Counting Fields with awk

ok, so a user can specify options as is shown below: ExA: cpu.pl!23!25!-allow or ExB: cpu.pl!23!25!-block!all options are delimited by the exclamation mark. now, in example A, there are 4 options provided by the user. in example B, there are 5 options provided by the user. ... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

awk finding counting sequence

Can awk count numbers until it reaches the end of the sequence after the slash? input: serv1a, 32, 41/47, 53, 89/100, 108/11, 113. serv1b, 1/2, 114/18, 121/35, 139/40, 143/55, 159/64, serv2, 255/56, 274/77, 763, 774/75, 777, 1434/35, 1444/50, 1715, 2025/31, 2048. serv10b, 804, 808, 929/32,... (9 Replies)
Discussion started by: sdf
9 Replies

6. Shell Programming and Scripting

awk, help me - counting items and listing them

This is my first ever post... please help! :o I have two columns....here is part of the file... 12, 46798 6692, 46799 5710, ... (3 Replies)
Discussion started by: pelhabuan
3 Replies

7. Shell Programming and Scripting

counting using awk

Hi, I want to perform a task using shell script. I am new to awk programming and any help would be greatly appreciated. I have the following 3 files (for example) file1: Name count Symbol chr1_1_50 10 XXXX chr3_101_150 30 YYYY File2: Name ... (13 Replies)
Discussion started by: Diya123
13 Replies

8. Shell Programming and Scripting

awk counting number of occurences

Hi, I am trying to count the max number of occurences of field1 in my apache log example: 10.0.0.1 field2 field3 10.0.0.2 filed2 field3 10.0.0.1 field2 field3 10.0.0.1 field2 field3 awk result to print out only the most occurence of field1 and number of occurence and field1 is... (3 Replies)
Discussion started by: phamp008
3 Replies

9. Shell Programming and Scripting

Counting with Awk

I need "awk solution" for simple counting! File looks like: STUDENT GRADE student1 A student2 A student3 B student4 A student5 B Desired Output: GRADE No.of Students A 3 B 2 Thanks for awking! (4 Replies)
Discussion started by: saint2006
4 Replies

10. Shell Programming and Scripting

Counting records with AWK

I've been working with an awk script and I'm wondeing id it's possible to count records in a file which DO NOT contain, in this instance fields 12 and 13. With the one script I am wanting to display the count for the records WITH fields 12 and 13 and a seperate count of records WITHOUT fields... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies
Login or Register to Ask a Question