cktb -- Check for trailing blanks (spaces, tabs) in a file is a perl program which checks for trailing blanks (space or tabs) ina file and reports the number found and the longest string of blanks found.

Source Code

# Look for and count the blanks or tabs at the end of lines
# --Kent Nassen, 5/26/99, 6/11/99, 6/28/00

# 6/28/00: only print longest string of whitespace message if the file
# had trailing whitespace.  Added run_time to header.

use FileHandle;
use Getopt::Std;

use strict;

use vars qw($file $filename $input $line $len $whitespace $match
  $matchcount $ProgName $version $pagesize $longest $longline $opt_l


$version="v1.1, 6/11/99";

($ProgName = $0) =~ s%.*/%%; # Unix
# ($ProgName = lc $0) =~ s%.*\\%%;  # DOS

die "\n*** $ProgName: need a filename to test. \n\
$ProgName: Find lines with trailing white space (tabs or spaces) in files,\
      by Kent Nassen, $version\n\
     Usage: $ProgName [-l] filename [filename...]\
            where -l gives long output, instead of a summary.\n\n"
        unless defined($file=$ARGV[0]);


# Print program header
if (!$opt_l) { print "   $ProgName: Find lines with trailing spaces or tabs,",
    " by Kent Nassen, $version\n   Report generated: $run_time\n";

# Check a file for trailing blanks/white space
foreach $file (@ARGV) {
	no strict 'refs';
    process($file, 'fh00');

sub process {
	no strict 'refs';
    # Print Header?
    local($filename, $input) = @_;
    unless (open $input, $filename) {
        print STDERR "\n  $ProgName: *** Can't open $filename: $!\n\n";
    while (<$input>) {
        if (/([\t\s]+)$/) {
            if (length($match)>$longest) {
                $longest=length($match); $longline=$line
            if ($opt_l) { write } # do long report (line# & count)
    } # end of while()
    if (!$matchcount) {
        print "\n   *** No lines found with trailing white space";
    print "\n   $line total lines in the file '$file'.\n";
    if ($matchcount) {
        print "   Longest string of trailing whitespace was $longest",
                " characters at line $longline.\n";
    close $input;

format STDOUT_TOP =


   "$ProgName: Find lines with trailing spaces or tabs, by Kent Nassen, $version";
   "Scanning for trailing white space in the file: ",$file
   "Report generated: ",$run_time

              Trailing WS
     Line#       Length
    -------   -----------

format STDOUT =
    @>>>>>> @>>>>>>>>>
$line, length($match)

Back to Kent's Perl Page