Solved Want to count No. of blank records for a particular column..

Dell Dell gaming backpack carrying case...
April 5, 2012 at 03:11:56
Specs: Windows XP, DDR
Consider,

1. I have a fixed width file with 3 column with their positions. (1 to 10, 11 to 20, 21 to 30)
2. I have total 100 records in a file.
3. Out of 100 records.. 40 records are Blank or null in column1 , 50 records are Blank or null in column2 and 60 records are Blank or null in column3..

4. so I want to make the count of blank or null for each particular column and create a output file.

5. output file shud contain : TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 1 : 40
TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 2 : 50
TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 3 : 60

Please help

Thanks
Mohammed Umar


See More: Want to count No. of blank records for a particular column..

Report •

#1
April 5, 2012 at 10:08:38
✔ Best Answer
How about a perl program using the substr function to do the parsing:


#!/usr/bin/perl

use warnings;
use strict;

my $src = "./fixfile.txt";
my $dest = "./outfixfile.txt";
my $tot1 = 0;
my $tot2 = 0;
my $tot3 = 0;

open(IN, '<', $src) or die "Cannot open inputfile $src: $!\n";
open(OUT, '>', $dest) or die "Cannot open outputfile $dest: $!\n";
while (<IN>) {
   chomp;
   # perl strings start counting from 0 not 1
   # column 1
   if (substr($_, 0, 10) eq "          ")
      {
      $tot1++;
      }

   # column 2
   if (substr($_, 10, 10) eq "          ")
      {
      $tot2++;
      }
 
  # column 3
   if (substr($_, 20, 10) eq "          ")
      {
      $tot3++;
      }
}
print OUT "TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 1 : $tot1 \n";
print OUT "TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 2 : $tot2 \n";
print OUT "TOTAL NUMBER OF BLANKS THAT OCCURRED IN column 3 : $tot3 \n";

close IN;
close OUT;


Report •

#2
April 19, 2012 at 04:31:14
It worked fine .. Thanks nails

Report •
Related Solutions


Ask Question