counts rows with numbers in columns
1
0
Entering edit mode
9.3 years ago
Kumar ▴ 170

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 • 4.0k views
ADD COMMENT
1
Entering edit mode
9.3 years ago
Adrian Pelin ★ 2.6k

How about

cat FILE | grep -P -v "\t0\t0\t0\t0" | wc -l
ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

This command showing:

cat: FILE: no such file or directory
ADD REPLY
2
Entering edit mode
Daemon, I want you to leave his body, and return to wherever you came from.
ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2008 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6