Re: Counting Identical lines in a file with BBEdit & Grep
Re: Counting Identical lines in a file with BBEdit & Grep
- Subject: Re: Counting Identical lines in a file with BBEdit & Grep
- From: John Delacour <email@hidden>
- Date: Sun, 27 Jul 2003 20:10:12 +0100
- Mac-eudora-version: 6.0a28
At 11:02 am -0700 27/7/03, Marconi wrote:
Given a text file containing multiple instances of about a dozen
different lines, is it possible, in BBEdit, to count the number of
times each unique line appears?
There's no need to use BBedit or any editor or any pattern matching.
Just read the lines from the file as keys of a hash (%h) and
increment the value each time you find the string. Here's an example
which creates the file and then does just that:
set _paras to paragraphs of "123456789
abcdefgh
123456789
opqrstuv
123456789
abcdefgh
dsltcpip
abcdefgh
123456789
abcdefgh
123456789
opqrstuv
abcdefgh
123456789
opqrstuv
opqrstuv"
(* CREATE SAMPLE FILE *)
set lf to ASCII character 10
set fU to "/tmp/f"
set f to fU as POSIX file
open for access f with write permission
set eof f to 0
repeat with _line in _paras
write _line & lf to f
end repeat
close access f
(* *)
do shell script "perl -e ' $f = qq~/tmp/f~; open F, $f;
for (<F>) { chomp ; $h{$_} += 1 }
for (sort keys %h) { print qq~$_ - $h{$_}\\n~ }'"
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.