FAQ
Hi,

I have a script which greps for a word in a file contains records.
I grabbed a particular column & sent the colomn values to a file.
I need to find each column value, the times it appeared in the file.
My script is:
grep sceneority <file> | cut -f 6 >> swi
I am stuck at how to find the occurance of column values in "swi" file.
Plz help me.
Ex:

The file has following column values:

123
324
123
123
435
435

The output should be

123 is 3 times
324 is 1 time
435 is 2 times

Thanks & Regards,
Pradeep.


Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/

Search Discussions

  • Pradeep reddy at Apr 5, 2008 at 6:18 pm
    Hi,

    I have a script which greps for a word in a file contains records..
    I grabbed a particular column & sent the colomn values to a file.
    I need to find each column value, the times it appeared in the file.
    My script is:
    grep sceneority <file> | cut -f 6 >> swi
    I am stuck at how to find the occurance of column values in "swi" file.
    Plz help me.
    Ex:

    The file has following column values:

    123
    324
    123
    123
    435
    435

    The output should be

    123 is 3 times
    324 is 1 time
    435 is 2 times

    Thanks & Regards,
    Pradeep.


    Bollywood, fun, friendship, sports and more. You name it, we have it on http://in.promos.yahoo.com/groups/bestofyahoo/
  • Pradeep reddy at Apr 5, 2008 at 6:18 pm
    Hi,

    I have a script which greps for a word in a file contains records..
    I grabbed a particular column & sent the colomn values to a file.
    I need to find each column value, the times it appeared in the file.
    My script is:
    grep sceneority <file> | cut -f 6 >> swi
    I am stuck at how to find the occurance of column values in "swi" file.
    Plz help me.
    Ex:

    The file has following column values:

    123
    324
    123
    123
    435
    435

    The output should be

    123 is 3 times
    324 is 1 time
    435 is 2 times

    Thanks & Regards,
    Pradeep.


    Share files, take polls, and make new friends - all under one roof. Go to http://in.promos.yahoo.com/groups/
  • Gunnar Hjalmarsson at Apr 5, 2008 at 6:59 pm

    pradeep reddy wrote:
    I am stuck at how to find the occurance of column values in "swi" file.

    The file has following column values:

    123
    324
    123
    123
    435
    435

    The output should be

    123 is 3 times
    324 is 1 time
    435 is 2 times
    Use a hash.

    open my $fh, '<', 'swi' or die $!;
    my %cnt;
    while (<$fh>) {
    chomp;
    $cnt{$_}++;
    }
    foreach my $val ( sort keys %cnt ) {
    print "$val is $cnt{$val} times\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
  • John W. Krahn at Apr 5, 2008 at 8:25 pm

    pradeep reddy wrote:
    Hi, Hello,
    I have a script which greps for a word in a file contains records.
    I grabbed a particular column & sent the colomn values to a file.
    I need to find each column value, the times it appeared in the file.
    My script is:
    grep sceneority <file> | cut -f 6 >> swi
    I am stuck at how to find the occurance of column values in "swi" file.
    Plz help me.
    Ex:

    The file has following column values:

    123
    324
    123
    123
    435
    435

    The output should be

    123 is 3 times
    324 is 1 time
    435 is 2 times
    my $file = '<file>';

    open my $FH, '<', $file or die "Cannot open '$file' $!";

    my %counts;
    while ( <$FH> ) {
    next unless /sceneority/;
    $counts{ ( split /\t/ )[ 5 ] }++;
    }

    for my $key ( sort { $a <=> $b } keys %counts ) {
    print "$key is $counts{$key} time", $counts{ $key } == 1 ? "\n" :
    "s\n";
    }




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
  • Pradeep reddy at Apr 6, 2008 at 12:09 am
    Hi,

    Thanks for the reply.

    Can this impletemented in shell script alsso?


    ----- Original Message ----
    From: Gunnar Hjalmarsson <noreply@gunnar.cc>
    To: beginners@perl.org
    Sent: Saturday, 5 April, 2008 3:01:41 PM
    Subject: Re: problem with the script in counting

    pradeep reddy wrote:
    I am stuck at how to find the occurance of column values in "swi" file.

    The file has following column values:

    123
    324
    123
    123
    435
    435

    The output should be

    123 is 3 times
    324 is 1 time
    435 is 2 times
    Use a hash.

    open my $fh, '<', 'swi' or die $!;
    my %cnt;
    while (<$fh>) {
    chomp;
    $cnt{$_}++;
    }
    foreach my $val ( sort keys %cnt ) {
    print "$val is $cnt{$val} times\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/


    Explore your hobbies and interests. Go to http://in.promos.yahoo.com/groups/
  • ken Foskey at Apr 6, 2008 at 12:27 am

    On Sun, 2008-04-06 at 05:39 +0530, pradeep reddy wrote:


    Can this impletemented in shell script alsso?
    Why do you ask this in a perl list?

    look at `uniq -c`.

    --
    Ken Foskey
    FOSS developer

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 5, '08 at 6:18p
activeApr 6, '08 at 12:27a
posts7
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase