Noame has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I execute the following command without get the error below: Command: $count=`ls -d 246.56* |wc -l `; Error Message: "246.56*: No such file or directory" Moreover, how can i 'count word' using Perl command? Thanks.

Replies are listed 'Best First'.
Re: Perl - 'count word'
by toolic (Bishop) on Nov 21, 2007 at 19:16 UTC
    Perhaps you could specify more clearly what you are trying to accomplish.

    The Unix piped command you mentioned, ls -d 246.56*  |wc -l, will count the number of entries (files, directories, etc.) in the current working directory whose names match 246.56* (for example, 246.56, 246.561, 246.56_foo). If no entries are found matching that name specification, then you get the No such file or directory error message.

    This is one way to count the number of entries while avoiding the error message:

    #!/usr/bin/env perl use warnings; use strict; my $count = 0; opendir my $dh, '.' or die "$!\n"; while (defined(my $entry = readdir($dh))) { $count++ if ($entry =~ /^246.56/); } closedir $dh; print "count=$count\n";

    If you are familiar with Unix commands but are just starting out with Perl, UNIX 'command' equivalents in Perl is a handy reference.

Re: Perl - 'count word'
by shmem (Chancellor) on Nov 21, 2007 at 23:08 UTC
    Count lines:
    perl -nle '}{print $.' file

    Count words:

    perl -lane '$c+=@F}{print $c' file

    Count files matching a pattern:

    perl -le '$_=()=glob(pop);print' '246.56*'

    See perlrun for command line switches. See glob. I'll explain that all if you want to know.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Perl - 'count word'
by igelkott (Priest) on Nov 21, 2007 at 19:20 UTC
    Just to be clear, your unix-like command counts the number of files and folders in the current working directory with names starting with "246.56". This is what you wanted, right?

    If you want to look in other folders or just want a little more control over what's going on, it may be useful to look into the builtin opendir and readdir functions or even move up to the File::Find or Find::File::Rule modules.

    For counting lines, there's clearly more than one way to do it, but a simple while(<F>){$words += split;} over each file gets it done (with a well placed my $words to count per file or cumulatively). I may get trashed for this last bit of sloppy code but it's a beginning.

    Update: I should learn to type faster. Didn't see that two similar answers showed up while chicken-scratching my own.

Re: Perl - 'count word'
by gleepglop (Novice) on Nov 21, 2007 at 19:08 UTC
    try putting a file with a name where the first 6 characters are 246.56 inside the same directory you run the script from. oh, nevermind, I see, you want to see how many files there are that match, and not get an error when the number is 0? First, I think maybe you should be using wc -w, not -l Second, I don't know, I don't know that much. But I'll try to think of something and come back if noone else does.
Re: Perl - 'count word'
by swampyankee (Parson) on Nov 22, 2007 at 07:31 UTC

    If all you're trying to do is count the number of files with names starting '246.56', in the program's pwd, I'd just use glob, mostly as it avoids setting off another process just to count files.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.