List Counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List Counter
# 1  
Old 02-29-2012
List Counter

Code:
 
cat sport_by_month
 
Month : Jan
sport :Football
sport :Volleyball
Month: Feb
sport :BasketBall
sport: Cricket
sport: Fotball
Month: Mar 
Month: APR
sport : Bowling
sport : Climbing

I need your help to have a script that count the number of sports per month and sorted by mont with more sport, so my output would be something like

Code:
 
./count.sh
 
Feb  3
Jan 2
APR 1
Mar  0

# 2  
Old 02-29-2012
Try:
Code:
awk '/Month/{m=$NF;A[m]=-1} {A[m]++} END{for(i in A)print i,A[i]}' infile | sort -rnk2

Code:
awk '/Month/{if(NR>1)print m,c; m=$NF; c=-1}{c++} END{print m,c}' infile | sort -rnk2


Last edited by Scrutinizer; 02-29-2012 at 07:53 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-29-2012
awk

Hi,

Try this one,

Code:
awk '/^sport/{a[p]++;}/^Month/{gsub(/Month *: */,"");p=$0;a[p]="0";}END{for ( i in a){print i,a[i];}}' Input_File

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 02-29-2012
In perl,

Code:
#!/usr/bin/perl
use Data::Dumper;

my %Hash = () ;
my ($fh , $mon , $name );
open $fh  ,"<file" or die "Can not open the file : $@";

while ( <$fh> ) {
     ($mon , $name ) = split ( ':',$_ ), chomp ( $name ) ,  $Hash{$name} = 0 , next   if ( /Month/ );
    $Hash{$name}++ if ( /sport/ ) ;
}

print Dumper \%Hash;

# 5  
Old 02-29-2012
Code:
$
$ cat sport_by_month
Month : Jan
sport :Football
sport :Volleyball
Month: Feb
sport :BasketBall
sport: Cricket
sport: Fotball
Month: Mar
Month: APR
sport : Bowling
sport : Climbing
$
$
$ perl -lne '/^(\w+)\s*:\s*(\w+)$/;
             $1 eq "Month" ? do{$m=$2; $x{$m}=0} : $x{$m}++;
             END{print "$_  $x{$_}" for sort {$x{$a} <=> $x{$b}}(keys %x)}
            ' sport_by_month
Mar  0
Jan  2
APR  2
Feb  3
$
$

tyler_durden
# 6  
Old 02-29-2012
Changing RS also does the trick.

Code:
$ awk 'NF>0{print $2,(gsub("\n",""))-1}' RS="Month" file
Jan 2
Feb 3
Mar 0
APR 2
$

# 7  
Old 02-29-2012
Note: this works for gawk and mawk, but not the other awks. POSIX awk only accepts a single character for RS.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do i miss counter or what

hello this script should show all users and space they used without problem : ls /home >> /root/users.txt cat /root/users.txt | while read line; do space=`du -s /home/$line` echo "$line space is $space" shift done but when i remove pipe ,script run without any output: ls /home... (4 Replies)
Discussion started by: nimafire
4 Replies

2. UNIX for Dummies Questions & Answers

Pegging counter

Hi Experts, I am in need for some help. My competence level on unix is not at all helping me to resolve this. Please help. My Input for a system command is as under: Counters are getting pegged each hour. I need to have a difference printed rather than pegged counter values. Counter... (2 Replies)
Discussion started by: vanand420
2 Replies

3. Shell Programming and Scripting

Counter

if ;then echo "mrnet greater 5000" gzip /var/log/mrnet.log mv /var/log/mrnet.log.gz /var/log/mrnet.log.1.gz if ];then i=1 let i++ mv /var/log/mrnet.log.1.gz /var/log/vieux-logs/mrnet.log.$i.gz else echo "theres no... (1 Reply)
Discussion started by: Froob
1 Replies

4. Shell Programming and Scripting

problem with counter

i having a file xxxxxxxxxxxxxxx1234 ...........value can be change xxxxxxxxxxxxxxx1235 xxxxxxxxxxxxxxxx1236 . . . . xxxxxxxxxxxxxxxxx1300 ...........value can be change i want to cut last four characters of first line and last line and find the missing pattern. output should... (4 Replies)
Discussion started by: sagar_1986
4 Replies

5. Shell Programming and Scripting

DelimiterCount: how to use a counter

Hi, I am new to shell script. I want to count to Delimiter count for my source file. For that I have written script. When I tried to execute the script I could not able to view the results. It throws errors. I don't know what the problem is. My aim is I want to store the delimiter count in one... (4 Replies)
Discussion started by: suresh01_apk
4 Replies

6. Shell Programming and Scripting

grep and counter

Hi, I have such an example(ksh): name1=Example directory=/directory1/Example/directory2 match=$(grep -s '$name1' $directory | wc -l) echo $match But it replies to me: 0 What I expect from it, is to find $name1 in $directory and produce 1 for me as true, not false. (3 Replies)
Discussion started by: chish
3 Replies

7. Shell Programming and Scripting

counter

Hi, I need some help. Shell script counter. i need to add condition to check if counter is more than 10 and longer than 3 hours? it runs every 5 mins. it only check count and send email right now. it runs in cron as below gregcount.ksh gregdb 10 > /tmp/gregcount.out 2> /tmp/gregcount.err ... (1 Reply)
Discussion started by: pega
1 Replies

8. Shell Programming and Scripting

counter problem

Hi, I'm attempting to take the following input list and create an output file as shown below. I've monkeyed around for long enough. Can anyone help? NOTE: fs*** will be header and I want to get a count on NY**. fs200a NY7A fs200b NY7B NY7B NY7B fs200c NY7C NY7C NY7C NY7C... (2 Replies)
Discussion started by: jwholey
2 Replies

9. Shell Programming and Scripting

counter in a variable - can it be done?

I have the following for(( i=1; 1<=2; i++)) do e1=123 n1=123 e2=456 n2=456 coord= $e1,$n1 echo "coordinate=$coord" done exit this echos coordinate=123,123 I need it to loop so: loop1 coord=$e1,$n1 loop2 (3 Replies)
Discussion started by: gazz1982
3 Replies
Login or Register to Ask a Question