Solved

Counting values in a list

Posted on 2013-11-17
8
213 Views
Last Modified: 2013-11-17
I know very little about PERL :(

I have list of millions of rows of data. All data values are separated by a comma

I have to count how many rows contain a specific data value

for example

101,102,143,145,146,149
101,102,143,145,147,148
101,102,143,145,147,149
101,102,143,145,148,149
101,102,143,146,147,148
101,102,143,146,147,149
101,103,143,146,148,149
101,103,143,147,148,149
101,103,144,145,146,147
102,104,144,145,146,148
102,104,144,145,146,149
102,104,144,145,147,148

from that sample list, I have to count how many rows have the value 101 for the first data entry, so for this list the number of rows with 101 in the first data entry would be 9

Could someone help me with a PERL script that will

1. open the file  (filename:  master.vim )
2. Count through all the rows and record the number of rows with the value at that data point - in the first column
3. write the recorded number of rows to another file ( count.txt )
4. close master.vim and count.txt

Thankyou very much in advance
0
Comment
Question by:MichaelGlancy
  • 4
  • 4
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39654634
perl -F, -alne '++$c if $F[0]==101;END{print $c}' master.vim > count.txt
0
 

Author Comment

by:MichaelGlancy
ID: 39654637
Thankyou ozo

is that to be run from the command line ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39654729
yes
0
 

Author Comment

by:MichaelGlancy
ID: 39654753
C:\Documents and Settings\Michael>perl -F, -alne '++$c if $
F[0]==101;END{print $c}' master.vim > count.txt

Can't find string terminator "'" anywhere before EOF at -e line 1.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MichaelGlancy
ID: 39654756
I have no idea what I am doing with PERL

Would it be possible for you to give me this information such that I can put it into a .pl file, that I can run in the same directory as the master file ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39654764
In msdos, you would quote the command line with " instead of '
perl -F, -alne "++$c if $F[0]==101;END{print $c}" master.vim > count.txt
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39654771
#!/usr/bin/perl
use strict;
use warnings;
open M,"<master.vim" or die "master.vim $!";
my $c=0;
$c+=/^101,/ while <M>;
close M;
open C,">count.txt" or die "count.txt $!";
print C "$c\n";
close C;
0
 

Author Closing Comment

by:MichaelGlancy
ID: 39654933
Great Solution

thankyou very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now