Question:
How to write a Perl script that can count and display unique line?
macausite
2006-06-19 22:44:33 UTC
How to write a perl script that can read every line from a file and count how many times each unique line was found.
Five answers:
sburlappp
2006-06-22 15:28:16 UTC
perl -e 'while (<>) {chomp; $z{$_}++;} while(($k,$v) = each(%z)) {print "$v\t$k\n";}' filetoscan.txt



Or, just do this instead:



cat filetoscan.txt | sort | uniq -c | sort -n
demaman
2006-06-20 00:13:04 UTC
That question is a bit complex and requires a long answer. Best bet look at some examples of Perl on google. Perl is a very powerful scripting language and can be used for things like that.



Good Luck
godown
2016-11-14 01:39:31 UTC
Perl Uniq
Ondrej
2015-05-26 00:00:35 UTC
The loop from the example above can be eliminated with the -n switch. Printing can be written simpler too:



perl -ne '{chop; $u{$_}++;} END {print $u{$_}."\t".$_."\n" for keys %u}' filetoscan.txt
2006-06-19 22:59:41 UTC
answers.google.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...