Removing Headers and a Column

try changing this:

    print substr $_,85;

to:

   print substr $_,84;

but it's a bit of a long shot.

No that didn't work. I am not really sure what is causing it to cut off that 4th line on column 5. Very strange.

Me either, substr($_,85) should slurp in everything after the 85 character until the end of the line.

Ah, I see, that's not what I had thought you were after really (thus it removing column 4 from the headers - I thought that was a requirement as I'd assumed the headers where column headers :rolleyes:).

It's not matching the "Organization Totals" string because it's looking for an exact line (the ^ and $ symbols mean start and end of line).

So, I believe you are looking for the following:

Remove characters 59-85 from each line,
EXCEPT:
Where the line is part of a repeating header block (6 lines of text every 52 lines)
Any line between the first instance of a line starting with the text "Organization Totals" and the end of the file

Do you still want the headers removed? (Assuming no for now)

So, the resulting code would then become:

#!/usr/bin/perl -w
$PAGESIZE=52;
$HEADERSIZE=6;
$linenumber=0;
$intotals=0;
while (<>) {
  $linenumber++;
  if (/^Organisation Totals/) {
    $intotals=1;
  }
  if ($intotals) {
    print $_;
  } elsif ($linenumber % $PAGESIZE > $HEADERSIZE) {
    # Not within the header block so cut column 4 out
    #  keep up to char 58, drop 27 chars, keep everything else
    if (/^(.{58}).{27}(.*)$/) {
      print "$1$2\n";
    } elsif (/^(.{58}).{1-27}$/) {
      print "$1\n";
    } else {
      print $_;
    }
  } else {
    # It's within the header so print it all
    #  if you want headers omitted, comment out the following line
    print $_;
  }
}

That script works for the most part, however the same problem happens as before. It is leaving column 4 in tact if it is the only thing on the line.

Not really to sure what else to say to give you a idea as to why it may be doing that.

change this line in Smiling Dragons code:

    if (/^(.{58}).{27}(.*)$/) {

to:

    if (/^(.{58}).{27}(.+)$/) {

and see if that helps. The problem is that .* means to match zero or more, so the elsif condition just below it might never get evaluated.

Adding the + just cuts off pieces of the header, while before the header was still in tact. It does not resolve the other problem with leaving column 4 around though.

Thanks again for all the help you two.

Long shot, a hybrid of my code and SM's code:

#!/usr/bin/perl
use strict;
use warnings;

while (<>) {
   # header lines
   if ($. < 7) {
      print; # comment out this line to delete header lines
      next;
   }
   # print the totals, assumes they are at the end
   if (/^Organisation Totals/) {
      print;
      print <>;
      exit;
   }
   # remove 4th column
   if (/^(.{58}).{27}(.+)$/) {
      print "$1$2\n";
   }
   # remove 4th column when its the only thing
   elsif (/^(.{58}).{1-27}$/) {
      print "$1\n";
   }
   else {
      # not sure what this might print if anything.
      # prefixed lines with '>>>' just to mark them for now	
      # to see if this condition is ever evaluated.
      print ">>>$_";
   }
}

That put >>> next to each line where column 4 is the only thing, however it still left the text there. Other than that it is working fine. It is marking >>> on completely blank lines as well though.

Thanks again

We need to check the length of the lines that only include column 4, do this:

   else {
      # not sure what this might print if anything.
      # prefixed lines with '>>>' just to mark them for now	
      # to see if this condition is ever evaluated.
      my $n = length($_):
      print "length = $n: [$_]";
   }

report back with the results. See whats inside the square brackets: [$_] there may be spaces at the end that need to be taken into account.

Or this may work:

#!/usr/bin/perl
use strict;
use warnings;

while (<>) {
   # header lines
   if ($. < 7) {
      print; # comment out this line to delete header lines
      next;
   }
   # print the totals, assumes they are at the end
   if (/^Organisation Totals/) {
      print;
      print <>;
      exit;
   }
   # remove 4th column
   if (/^(.{58}).{27}(.+)$/) {
      print "$1$2\n";
   }
   # remove 4th column when its the only thing
   elsif (/^\s{58}).{1-27}\s*$/) {
      print "$1\n";
   }
   else {
      # not sure what this might print if anything.
      # prefixed lines with '>>>' just to mark them for now	
      # to see if this condition is ever evaluated.
      print ">>>$_";
   }
}

this explicitly checks for 58 spaces at the beginning then 1 to 27 charcaters (4th column) and then zero or more spaces. Run it and see if the >>> marker appears or if does what you need.