in reply to problem count the number of words

Hello,

Given what wise haukex already said and considering only your title, a simple oneliner can do the task (pay attention to windows double quotes):

perl -lne "$count+=split}{print $count" /path/file1 /path/file2

Using Deparse can give you a working starting point to work with:

perl -MO=Deparse -lne "$count+=split}{print $count" /path/file1 /path +/file2 BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; $count += split(' ', $_, 0); } { print $count; } -e syntax OK

That can be translated in: foreach file in the input ( see ARGV in perlodoc ) read it line by line, chomp each line, split the line at withespaces and add the resulting word count to $count When all file processing is finished print the value of $count

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: problem count the number of words -- oneliner
by kschwab (Vicar) on Dec 26, 2018 at 16:40 UTC
    Here's an alternative:
    perl -ne 's/\W+/$i++/eg;END{print "$i\n"}' f1 f2
      Hi kschwab,

      Are you sure? This one gives the same result like wc.

      perl -ne 's/\S+/$i++/egi;END{print "$i\n"}' f1 f2
        Guess it depends on what OP means by "all words", but sure.
        # works like wc perl -ne 's/\s+/$i++/eg;END{print "$i\n"}' f1 f2
        Not sure why you have the "i" modifier.