0

I want to generate an output file that shows the frequency of each word inside an input file. After some search, I found that Perl is the ideal language for this problem, but I don't know this language.

After some more search, I found the following code here at stackoverflow, supposedly it provides the solution I want at great efficiency:

perl -lane '$h{$_}++ for @F; END{for $w (sort {$h{$b}<=>$h{$a} || $a cmp $b} keys %h) {print "$h{$w}\t$w"}}' file > freq

I tried running this command line using the form below:

perl -lane 'code' input.txt > output.txt

The execution halts due to an unexpected '>' (the one at '<=>'). I did some research but can't understand what is wrong. Could some one enlight me? Thanks!

Here is the topic from where I got the code: Elegant ways to count the frequency of words in a file

If it's relevant, my words use letters and numbers and are separated by a single white space.

Dada
  • 6,313
  • 7
  • 24
  • 43
pedmacedo
  • 3
  • 2
  • You might want to check out this article on Perl Maven, explaining how to write a script to produce word frequencies. https://perlmaven.com/count-words-in-text-using-perl – daotoad May 20 '19 at 19:55
  • 1
    What's the actual error message? – melpomene May 20 '19 at 20:10
  • 1
    Possible duplicate of *[Why doesn't my Perl one-liner work on Windows?](https://stackoverflow.com/questions/660624/why-doesnt-my-perl-one-liner-work-on-windows)* – Peter Mortensen Jul 12 '19 at 19:02

1 Answers1

5

You are probably using Windows. You therefore need to use doubles quotes " instead of singles quotes ' around your code:

perl -lane "$h{$_}++ for @F; END{for $w (sort {$h{$b}<=>$h{$a} || $a cmp $b} keys %h) {print qq($h{$w}\t$w)}}" file > freq

Also, note how I used qq() instead of "..." within the code, as suggested by @mob. Another option is to escape the quotes with \".

Dada
  • 6,313
  • 7
  • 24
  • 43
  • Will escaping the quotes work? If not, the `qq{}` syntax offers a workaround (`print qq[$h{$w}\t$w]`) . – mob May 20 '19 at 21:12
  • @mob, I tried it on a Windows and it worked, but I'm not sure it works in every situation though. Gonna edit your `qq{}` suggestion in ;) – Dada May 20 '19 at 22:09