bash programming question

John M Phillips yellowdog-general@lists.terrasoftsolutions.com
Sun Jan 25 20:18:01 2004


While shell scripts are quit powerful, for myself your application crosses
the line to where I would use perl.  Pattern searches for "^#" are quite
easy in perl.

The example below gets the line counts for a single file.  If you create
this as an executable /usr/local/bin/cmmtCount, then the contents of /etc
can be listed using

 find /etc -type f -exec cmmtCount {} \;

John Phillips

-----------------------------------------------------
#!/usr/bin/perl -w
                                                                                
    my $lineTotal = 0;          # count of total lines
    my $lineCmmt  = 0;          # count of lines with leading #
    my $file      = $ARGV[0];   # file name
                                                                                
    if (open(FILE,$file)) {
      while ($line=<FILE>) {
        $lineTotal += 1;
        if ($line =~ m/^#/) {
          $lineCmmt += 1;
        }
      }
      print "$file $lineTotal / $lineCmmt\n";
      close(FILE);
    }
    else {
      print "$file not openned\n";
    }


On 23 Jan 2004, green8 wrote:

> i want to display all the files in folder /etc that i can read, i mean i 
> want to display their names and two numbers next to each of them: their 
> number of lines, and number of lines that NOT start with "#". PLEASE 
> tell  me how to do it just this one time, i won't manage it by myself, 
> thanks, Barti, Poland
> _______________________________________________
> yellowdog-general mailing list
> yellowdog-general@lists.terrasoftsolutions.com
> http://lists.terrasoftsolutions.com/mailman/listinfo/yellowdog-general
>