# array problem

**URL:** <https://community.unix.com/t/array-problem/168990>\
**Category:** Shell Programming and Scripting\
**Created:** [February 8, 2007, 11:33pm UTC](https://community.unix.com/t/array-problem/168990 "2007-02-08T23:33:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_22734026db12803ebb3e059622ac3534.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [February 8, 2007, 11:33pm UTC](https://community.unix.com/t/array-problem/168990/1 "2007-02-08T23:33:26Z")

</div>

Dear Experts,

please help me out once again my array concepts is not very clear i have one text file like.  
1|usa|hh  
2|usa|ll  
3|usa|vg  
4|uk|nn  
5|uk|bb  
6|kuwait|mm  
6|kuwait|jkj  
7|dubai|hh

i want to store the third fied of a text file in he array and after that it should give me some reports like this

third field count  
usa 3  
uk 2  
kuwait 2  
dubai 1  
it should store the third field in to the array and give me the count how many times any name appeared.the third field can be alphanumeric.  
please can any body tell me how to do this using array.

Regards,  
Shary

---

<div class="post-metadata">

**Author:** ![sumeet](https://community.unix.com/user_avatar/community.unix.com/sumeet/32/3503_2.png) [@sumeet](https://community.unix.com/u/sumeet)\
**Post date:** [February 9, 2007, 12:15am UTC](https://community.unix.com/t/array-problem/168990/2 "2007-02-09T00:15:25Z")

</div>

try this:

even I have been looking for a awk script for that its something like  
group by columns and then the count.

I have awk to do group by and the sum:

awk -F, '{sum[$1,$2,$3]+=$4} END {for (i in sum) print i, sum[i]}' data.

Thanks  
SUmeet

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_22734026db12803ebb3e059622ac3534.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [February 9, 2007, 1:05am UTC](https://community.unix.com/t/array-problem/168990/3 "2007-02-09T01:05:12Z")

</div>

Dear Summit,

thank you so much for replying me i really appreciate you  
but please can you expalin me this  
{sum[$1,$2,$3]+=$4}  
what does this mean in array coz my array concept is weak and the other thing i want to store only 3rd field but in your case it seems to be stored 1,2,3 fileds right .  
the data might be 1000 line in my text file so please can you brief it out your command  
awk -F, '{sum[$1,$2,$3]+=$4} END {for (i in sum) print i, sum[i]}' data.

Regards,  
Shary

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 9, 2007, 2:33am UTC](https://community.unix.com/t/array-problem/168990/4 "2007-02-09T02:33:48Z")

</div>

A perl solution:

```nohighlight
$ cat data
1|usa|hh
2|usa|ll
3|usa|vg
4|uk|nn
5|uk|bb
6|kuwait|mm
6|kuwait|jkj
7|dubai|hh
$
$
$ cat mm.pl
#! /usr/local/bin/perl

open(DATA, "< data") || die "Unable to open file other\n";
while (<DATA>) {
        chomp;
        @fields = split(/\|/);
        $counts{$fields[1]}++;
}
close(DATA);

foreach $word (sort keys %counts) {
        print "value = ", $word, " count = ", $counts{$word}, "\n";
}
exit 0
$
$
$
$ ./mm.pl
value = dubai count = 1
value = kuwait count = 2
value = uk count = 2
value = usa count = 3
$

```

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_22734026db12803ebb3e059622ac3534.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [February 9, 2007, 3:10am UTC](https://community.unix.com/t/array-problem/168990/5 "2007-02-09T03:10:33Z")

</div>

Dear Experts,

i dont want the script in perl.  
so please if its possible try to do other than perl using arrays(not perl).

Regards,

Shary

---

<div class="post-metadata">

**Author:** ![tayyabq8](https://community.unix.com/user_avatar/community.unix.com/tayyabq8/32/483_2.png) [@tayyabq8](https://community.unix.com/u/tayyabq8)\
**Post date:** [February 10, 2007, 1:30am UTC](https://community.unix.com/t/array-problem/168990/6 "2007-02-10T01:30:55Z")

</div>

As per your sample output, you need count of second field not the third one, try this:

```nohighlight
awk -F"|" '{ freq[$2]++ } END {
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}' data

```

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_22734026db12803ebb3e059622ac3534.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [February 10, 2007, 4:48am UTC](https://community.unix.com/t/array-problem/168990/7 "2007-02-10T04:48:11Z")

</div>

dear experts,  
thank uou so much it really works.

take care bye  
shary
