Dear fellow Monks,
the standard *nix command
strings returns a list of printable strings from an input file.
I want to make an enhanced version of that command, returning only the words (not strings) that appear more than once.
I came up with this code:
#!/usr/bin/perl -n
END{print map{$s{$_}>1?"$s{$_}\t$_$/":""}keys%s}$s{$1}++while/(\w{4,}\
+b)/g
76 characters. Not bad for a short script. Too long if I want to make it a one-liner.
I gave myself a few rules:
- The script should work with Perl 5.6.1;
- The script should output only the words that appear more than once;
- One string per line;
- No modules. I need it to work in any platform, without rushing to the CPAN;
- No system calls;
- The output should be tab-separated and sortable through the standard sort command;
- Only sequences of 4 or more printable characters should be returned;
The above script, executed as
perl strings.pl `which perl` | sort -rn
will return:
15 DynaLoader
6 Usage
4 UWVS
2 dl_unload_file
2 dl_undef_symbols
2 version
2 dl_install_xsub
2 GLIBC_2
2 boot_DynaLoader
2 dl_find_symbol
2 filename
2 linux
2 dl_error
2 dl_load_file
Any suggestions to shorten it?
TIA
update Please notice that the output of 'strings' is a stream of printable characters (could be a word or several space-separated words), while I am interested in getting the unique words only.
_ _ _ _
(_|| | |(_|><
_|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.