Solved

counting rows in a list 5

Posted on 2013-11-17
4
186 Views
Last Modified: 2013-11-19
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
101,104,144,145,146,148
101,104,144,145,146,149
101,104,144,145,147,148
101,105,144,145,147,148

from that sample list, I have to count how many rows have the value 103 for the second data entry, but only if the value in the first column is 101, so for this list the number of rows with 103 in the second data entry where the first value is 101, would be 1

I have a previous script (attached) from "jb1dev + ozo" which counts the rows with particular values in the first and second column.

Now the script works great to count as above.

I need this script to count every row with 101 and list the number of rows that contain

101,102
101,103
101,104
101,105

and list them in the output file as (according to the above list)

102 6
103 3
104 3
105 1

Could you alter this script (as it works very fast) to count the number of rows with 101 in the first column and record the number of rows with the variable value in the second column ?

Thankyou very much in advance
count3a.txt
0
Comment
Question by:MichaelGlancy
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39655208
perl -ne "$c{$1}++ if /^101,(\d+)/;END{print "$_ $c{$_}\n" for sort{$c{$b}<=>$c{$a}}keys %c}" master.vim > count.txt
0
 

Author Comment

by:MichaelGlancy
ID: 39655232
thanks ozo, could I have it as a .pl file again please ?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39655264
#!/usr/bin/perl
use strict;
use warnings;
open M,"<master.vim" or die "master.vim $!";
my %c;
 /^101,(\d+)/ &&  $c{$1}++ while <M>;
close M;
open C,">count.txt" or die "count.txt $!";
print C "$_ $c{$_}\n" for sort{$c{$b}<=>$c{$a}}keys %c;
close C;
0
 

Author Closing Comment

by:MichaelGlancy
ID: 39659289
Fantastic, works great, 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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

23 Experts available now in Live!

Get 1:1 Help Now