Home » Infrastructure » Unix » using arrays
using arrays [message #218647] Fri, 09 February 2007 02:40 Go to next message
shary
Messages: 15
Registered: May 2006
Location: KUWAIT
Junior Member

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.
and i dont want perl to be used it should be simple unix script

Regards,
Shary
Re: using arrays [message #220547 is a reply to message #218647] Wed, 21 February 2007 03:55 Go to previous message
KCee
Messages: 16
Registered: October 2006
Junior Member
#!/bin/ksh

# 2007 Kees D. Hottinga

value=""
valnr=0

echo "third field count"

# Sort the field by the 2nd column.
sort -t\| -k2 test.txt | while read line
do
field2=`echo $line | cut -d'|' -f2`
if [ "$field2" != "$value" ]
then
# New value found.
if [ "$value" != "" ]; then echo "$value $valnr" ; fi
value=$field2
valnr=0
fi
valnr=`expr $valnr + 1`
done

# Last results.
if [ $valnr -gt 0 ] ; then echo "$value $valnr" ; fi
Previous Topic: xwindows
Next Topic: Unix Shell Script - No of users connected to oracle database
Goto Forum:
  


Current Time: Sat Dec 03 14:26:15 CST 2016

Total time taken to generate the page: 0.07459 seconds