Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Counting values in a list

Posted on 2013-11-17
Medium Priority
222 Views
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
Question by:Europa MacDonald
• 4
• 4

LVL 85

Expert Comment

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

Author Comment

ID: 39654637
Thankyou ozo

is that to be run from the command line ?
0

LVL 85

Expert Comment

ID: 39654729
yes
0

Author Comment

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

Author Comment

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 85

Expert Comment

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 85

Accepted Solution

ozo earned 2000 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

ID: 39654933
Great Solution

thankyou very much
0

## Featured Post

Question has a verified solution.

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

When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
###### Suggested Courses
Course of the Month13 days, 9 hours left to enroll

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

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