The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array problem ajgwin Shell Programming and Scripting 0 05-27-2008 09:36 AM
problem with array=($(find ....) jul Shell Programming and Scripting 1 02-27-2008 04:28 PM
problem with the blank spaces in array kamicasse UNIX for Dummies Questions & Answers 4 12-06-2007 02:40 PM
Point and Array Convert Problem zhshqzyc High Level Programming 0 02-25-2006 09:28 PM
char array problem DebianJ High Level Programming 4 06-25-2005 10:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-09-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
array problem

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
  #2 (permalink)  
Old 02-09-2007
sumeet sumeet is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 31
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
  #3 (permalink)  
Old 02-09-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
array problem

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
  #4 (permalink)  
Old 02-09-2007
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
A perl solution:
Code:
$ 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
$
  #5 (permalink)  
Old 02-09-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
using arrays

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
  #6 (permalink)  
Old 02-10-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
As per your sample output, you need count of second field not the third one, try this:
Code:
awk -F"|" '{ freq[$2]++ } END {
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}' data
  #7 (permalink)  
Old 02-10-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
using array

dear experts,
thank uou so much it really works.

take care bye
shary
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0