Solved

Counting values in a list

Posted on 2013-11-17
8
212 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 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

10 Experts available now in Live!

Get 1:1 Help Now