• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Counting Identical lines in a file with BBEdit & Grep
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Counting Identical lines in a file with BBEdit & Grep


  • Subject: Re: Counting Identical lines in a file with BBEdit & Grep
  • From: kai <email@hidden>
  • Date: Mon, 28 Jul 2003 10:13:55 +0100

on Sun, 27 Jul 2003 11:02:48 -0700, Marconi <email@hidden> 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?
>
> Given:
>
> 123456789
> abcdefgh
> 123456789
> opqrstuv
> 123456789
> abcdefgh
> dsltcpip
> abcdefgh
> 123456789
> abcdefgh
> 123456789
> opqrstuv
> abcdefgh
> 123456789
> opqrstuv
> opqrstuv
>
> can BBEdit be scripted to distill it to:
>
> 6: 123456789
> 5: abcdefgh
> 4: opqrstuv
> 1: dsltcpip
>
> where the number at each line start is the count...

If you don't mind plain ol' vanilla, you could do it with AppleScript's text
item delimiters - something like this:

==========================

to countIdenticalLines from l
set c to {}
set d to ASCII character 1
set text item delimiters to return
set l to l's text items 1 thru -2
set text item delimiters to d & d
set l to d & l & d
repeat until (count l) is 0
set text item delimiters to d
set w to l's text item 2
set text item delimiters to d & w & d
set l to l's text items
set c's end to ((count l) - 1 as string) & ": " & w
set text item delimiters to ""
set l to l as string
end repeat
set text item delimiters to return
set c to (c as string) & return
set text item delimiters to {""}
c
end countIdenticalLines

set x to "123456789
abcdefgh
123456789
opqrstuv
123456789
abcdefgh
dsltcpip
abcdefgh
123456789
abcdefgh
123456789
opqrstuv
abcdefgh
123456789
opqrstuv
opqrstuv
"

countIdenticalLines from x

==========================

--> "6: 123456789
--> 5: abcdefgh
--> 4: opqrstuv
--> 1: dsltcpip
--> "

Such a routine avoids the need to iterate through every individual line. In
the above case, for example, the loop repeats only 4 times (once for each
unique occurrence of a string).

---
kai
_______________________________________________
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.

  • Prev by Date: Re:Mail Print GUI
  • Next by Date: Re: Retrieving Rich Text from a file
  • Previous by thread: Re: Counting Identical lines in a file with BBEdit & Grep
  • Next by thread: Re: Counting Identical lines in a file with BBEdit & Grep
  • Index(es):
    • Date
    • Thread