Frank Schönmann: Zeilen aller Dateien eines Verzeichnisses zaehlen

Beitrag lesen

hi!

Problem: ich moechte gerne alle Zeilen von Dateien eines bestimmten types in einem Verzeichnis Zaehlen...

Ich hab mal ein kleines Perl-Programm zum ziemlich genauen zählen der effektiven Sourcezeilen geschrieben,
vielleicht kannst du damit was anfangen (funktioniert zu Zeit für Perl und C++ Kommentare).

=== cut ===
#!/usr/bin/perl

cl.pl v1.27 - (c) Copyright by Frank Schoenmann fs@tower.de, 2000.

Count lines in a range of files

if (!@ARGV)
{
    print <<EOT;

Usage: cl.pl [-C] [-B] <files>

-C   do not count lines which are (perl or c++) comments
  -B   do not count blank lines
EOT
    exit;
}

for (@ARGV)
{
    if (/-C/) { $nocomments++; }
    if (/-B/) { $noblanklines++; }
    if (/-CB/ /-BC/) { $nocomments++; $noblanklines++; }
}

if (!<$ARGV[$#ARGV]>)
{
    print "Error: File(s) '$ARGV[$#ARGV]' not found.";
    exit;
}

for (<pop $ARGV[$#ARGV]>)
{
    if (-T $_)
    {
        open FILE, $_;
        while (<FILE>)
        {
            $lines++;

if ($nocomments)
            {
                if (/^\s*#/) { $lines--; $comments++; }
                if (/^\s*///) { $lines--; $comments++; }
                $update++;
            }
            if ($noblanklines)
            {
                if (/^\s*$/) { $lines-- if $update; $blanklines++; }
            }
            $update--;
        }
        close FILE;
    }
}

print "$lines Zeilen";

$nocomments && $noblanklines && print " ohne Kommentare und Leerzeilen (", $comments + $blanklines, " Zeilen)";
$noblanklines $nocomments && print " ohne Kommentare ($comments Kommentarzeilen)";
$nocomments $noblanklines && print " ohne Leerzeilen ($blanklines Leerzeilen)";
=== cut ===

Funktioniert bei mir ziemlich gut, und dürfte auch so ziemlich alle reinen Kommentarzeilen und Leerzeilen
rausfiltern.

bye, Frank!