Question: counts rows with numbers in columns
0
gravatar for Manoj
2.0 years ago by
Manoj20
Canada
Manoj20 wrote:

Hi

I have a multiple column text file, i need to count rows those have only numbers (1....100) in columns [2], [3], [4], [5], else ignore if columns [2], [3], [4], [5] have 0 numbers like yellow. i am trying a following Perl script ..

#########################

file example

#####################

AG123.4     1234  1     3     4     1

AG123.5     3456  0     0     0     0

AG654.8     9876  4     1     0     0

AG654.7     9999  0     0     1     0

#############################

perl script

############################

open(FILE, "<all.txt") or die "Could not open file: $!";

my @lines = 0;
my @column=0;

my $count1=0;
my $count2=0;

while (<FILE>) {
    chomp;
    $lines++;
    $column= split (/\t/);
    $column++;
    if (@column==0) {
        $count1++;
    }
    elsif (@column>0)
    {
        $count2++;
    }
  
}
print("lines=$lines column=$column\n");
print "total number of zero in column 1:=", $count1[1],"\n";
print "total number of greater number of zero in column 1:=", $count2[1],"\n";
print "total number of zero in column 2:=", $count1[2],"\n";
print "total number of greater number of zero in column 2:=",$count2[2],"\n";
print "total number of zero in column 3:=", $count1[3],"\n";
print "total number of greater number of zero in column 3:=", $count2[3],"\n";
alignment • 955 views
ADD COMMENTlink modified 2.0 years ago • written 2.0 years ago by Manoj20
1
gravatar for Adrian Pelin
2.0 years ago by
Adrian Pelin2.0k
Canada
Adrian Pelin2.0k wrote:

How about

cat FILE | grep -P -v "\t0\t0\t0\t0" | wc -l

ADD COMMENTlink written 2.0 years ago by Adrian Pelin2.0k

thanks for reply. Moreover, i would like extend my query like i need to count total numbers in different columns such as total frequency of 1 in column [2], [3], [4] and [5], total frequency of 2 in column [2], [3], [4] and [5], total frequency of ........in [2], [3], [4], [5]. if all these columns does have 0 ignore these rows....

 

 

 

ADD REPLYlink written 2.0 years ago by Manoj20

Since it looks like you're trying to learn Perl, I'll give you the logic and see if you can write the code.

1) Create four hashes (one for each column) to contain the output you want. 2) Iterate over each line of your file. 3) Create a regular expression to match the line of data. 4) Test if all four columns contain zero; if so, skip to the next line. 5) If not, assign the value in each column as the key for the corresponding hash, and increment the value. 6) After parsing the entire file, print key/value pairs for each of the four hashes.

 

 

ADD REPLYlink modified 2.0 years ago • written 2.0 years ago by harold.smith.tarheel3.8k

this command showing: cat: FILE: no such file or directory

ADD REPLYlink written 2.0 years ago by Manoj20
2
Daemon, I want you to leave his body, and return to wherever you came from.
ADD REPLYlink written 2.0 years ago by Pierre Lindenbaum98k

That's because the name of your file is not 'FILE'.

ADD REPLYlink written 2.0 years ago by harold.smith.tarheel3.8k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1396 users visited in the last hour