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 > freqI tried running this command line using the form below:
perl -lane 'code' input.txt > output.txtThe 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.
- You might want to check out this article on Perl Maven, explaining how to write a script to produce word frequencies.perlmaven.com/count-words-in-text-using-perldaotoad– daotoad2019-05-20 19:55:11 +00:00CommentedMay 20, 2019 at 19:55
- 1What's the actual error message?melpomene– melpomene2019-05-20 20:10:59 +00:00CommentedMay 20, 2019 at 20:10
- 1Possible duplicate ofWhy doesn't my Perl one-liner work on Windows?Peter Mortensen– Peter Mortensen2019-07-12 19:02:07 +00:00CommentedJul 12, 2019 at 19:02
1 Answer1
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 > freqAlso, note how I usedqq() instead of"..." within the code, as suggested by @mob. Another option is to escape the quotes with\".
Explore related questions
See similar questions with these tags.