This is a test version of Biostars. For the public version, visit https://www.biostars.org.
counts rows with numbers in columns

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

1 answer

How about

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

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....

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.

This command showing:

cat: FILE: no such file or directory
Daemon, I want you to leave his body, and return to wherever you came from.

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

Log in to answer this question.