Counting feilds entries with Perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting feilds entries with Perl
# 1  
Old 04-01-2011
Counting feilds entries with Perl

Hi All,
I have a small problem of counting the number of times a particular entry that exists in a horizontal string of elements and a vertical feild (column of entries). For example
Code:
AATGGTCCTG

Expected output
Code:
A=2 C=2 G=3 T=3

I have an idea to do this but I dont know how to do that if these entries occur in like
Code:
A
A
T
G
G
T
C
C
T
G

Same expected output as above desired.

Could someone suggest an idea ? I'm learning Perl so I would appreciate your input.

CheersSmilie

---------- Post updated at 11:50 AM ---------- Previous update was at 11:05 AM ----------
I found a solution already !!
Hv a nice weekend Smilie

Last edited by pludi; 04-01-2011 at 01:57 PM..
# 2  
Old 04-01-2011
Given your data is in 'file.txt':
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

my ( $data, %count );

{

    # Slurp in all data at once
    local $/;
    open my $fh, '<', 'file.txt' or die "Can't open file.txt: $!";
    $data = <$fh>;
    close $fh;
}

# Remove any line breaks, spaces, tabs, ...
$data =~ s/[\s]//msg;

# Split between each character, and sum the occurences
$count{$_}++ foreach ( split( //, $data ) );

# Print a sorted list of characters and their occurences
print "$_=$count{$_} " foreach sort keys %count;

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Word counting perl script

Hi friends i need a help on Perl Script In My Home directory, i have some other directories and inside those directories i have some subdirectories and all the directories contains files. Now i want to count a word in all files and i want the output like below wordcount in which file(name... (5 Replies)
Discussion started by: siva kumar
5 Replies

3. Shell Programming and Scripting

Perl: Counting matches

Hi, A perl newbie here so pretty sure it's something simple. Trying to figure out how to count matches with perl pattern matching. The following script opens a text data file and finds lines containing "PORT:" and I'd like to count how many of these are found. Any ideas? open(LOG,"<... (3 Replies)
Discussion started by: hdefjunkie
3 Replies

4. Shell Programming and Scripting

Counting entries in a file

Hi, I have a very large two column log file in the following format: # Epoch Time IP Address 899726401 112.254.1.0 899726401 112.254.1.0 899726402 154.162.38.0 899726402 160.114.12.0 899726402 165.161.7.0 899726403 ... (39 Replies)
Discussion started by: sajal.bhatia
39 Replies

5. Shell Programming and Scripting

counting words with perl?

how to use perl to count number of lines, words characters in a file. (3 Replies)
Discussion started by: winter9
3 Replies

6. Shell Programming and Scripting

Counting

Hi, The following output shows how many pmon process are started by users named : oracle or yoavb $ ps -ef |grep pmon |grep -v grep |grep -v ipmon oracle 11268 1 0 Sep 2 ? 36:00 ora_pmon_qerp oracle 17496 1 0 Oct 11 ? 8:58 ora_pmon_bcv oracle 15081 1 0 ... (5 Replies)
Discussion started by: yoavbe
5 Replies

7. Shell Programming and Scripting

Counting duplicate entries in a file using awk

Hi, I have a very big (with around 1 million entries) txt file with IPv4 addresses in the standard format, i.e. a.b.c.d The file looks like 10.1.1.1 10.1.1.1 10.1.1.1 10.1.2.4 10.1.2.4 12.1.5.6 . . . . and so on.... There are duplicate/multiple entries for some IP... (3 Replies)
Discussion started by: sajal.bhatia
3 Replies

8. Shell Programming and Scripting

Counting multiple entries in a file using awk

Hi, I have a big file (~960MB) having epoch time values (~50 million entries) which looks like 897393601 897393601 897393601 897393601 897393602 897393602 897393602 897393602 897393602 897393603 897393603 897393603 897393603 and so on....each time stamp has more than one... (6 Replies)
Discussion started by: sajal.bhatia
6 Replies

9. Shell Programming and Scripting

Counting

Hi, I want to count how many rows are in a file for a specific column. eg. K NM K NM K NM K JK K NM K JK K NM so the file is tab-delimited. I want to count how many rows are in column 2 and how many NMs there are. I used awk awk '{OFS="\t"}; {count++} {print i,... (3 Replies)
Discussion started by: phil_heath
3 Replies

10. Shell Programming and Scripting

need to insert two columns (or two feilds) at the begining of the table

Hey guys/gals, So here is what i'm starting with...its a Solaris patch diag output. bash-3.0# pca -l Using /var/tmp/patchdiag.xref from Oct/20/08 Host: seiftsx4140 (SunOS 5.10/Generic_127128-11/i386/i86pc) List: missing Patch IR CR RSB Age Synopsis ------ -- - -- --- ---... (3 Replies)
Discussion started by: zeekblack
3 Replies
Login or Register to Ask a Question