Case statement w/count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case statement w/count
# 1  
Old 05-19-2010
Case statement w/count

Hello all,

I am trying to create a script that can read a file and produce a count per date (such as a case statement of some kind):

This is a sample of the data:

Code:
05-01 02
05-01 02
05-01 02
05-01 02
05-01 02
05-01 02
05-01 02
05-01 03
05-01 03
05-01 03
05-01 03
05-01 03
05-01 03
05-01 03
05-01 03
05-01 03
05-01 04
05-01 04
05-01 04
05-01 04
05-01 04

What i want to return is something like this:

Code:
05-01 02 count 7
05-01 03 count 9
05-01 04 count 5
etc...

Any suggestions?

Last edited by Scott; 05-19-2010 at 07:33 PM.. Reason: Code tags please...
# 2  
Old 05-19-2010
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $infile='infile';
my @arr=();

open(IF,$infile) or die "Error opening infile $infile: $!\n";
while (<IF>) {
chomp($_);
push @arr, $_;
}
close(IF);

    my %count;
    map { $count{$_}++ } @arr;
    map {print "$_ count ${count{$_}}\n"} sort keys(%count);

# 3  
Old 05-19-2010
sadly it would seem that perl is not installed on this server...
# 4  
Old 05-19-2010
Smilie
Are you sure? What is the output of
Code:
perl -v

However I'm sure it won't take too long and you will get an appropriate awk variant.
# 5  
Old 05-19-2010
My suggestion is to try awk instead, if you don't have perl.

What have you tried so far - and what does it have to do with case statements?
# 6  
Old 05-19-2010
ok... my apologizes... perl is there

"This is perl, v5.8.5 built for i386-linux-thread-multi"

I guess I am not sure how to actually run the suggested script that was provided pseudocoder (i am a totally rookie running perl of any kind (i.e. I have never done this before))

How do i run the suggested script?
# 7  
Old 05-19-2010
All right..

1. copy the script from above and store it in a fresh file
2. save the file as e.g. checkcount.pl
3. adjust the my $infile='infile'; line and put the path to your inputfile between the single quotes.
if your .pl file is in the same directory as your inputfile, then you only need the name of the inputfile.
4. run the script like so: perl checkcount.pl

If it works, then you should adjust the shebang line (#!/usr/local/bin/perl) and put the appropriate path to your perl binary (which will most likely be /usr/bin/perl). Check it with which perl
From now on you can run you script like so: ./checkcount.pl

Hope I was clear and that it helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

4. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

5. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

6. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

7. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

8. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies

9. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question