Re: Finding lines containing foo in a file
Re: Finding lines containing foo in a file
- Subject: Re: Finding lines containing foo in a file
- From: deivy petrescu <email@hidden>
- Date: Tue, 20 Dec 2005 21:03:14 -0500
On Dec 20, 2005, at 20:05, Stockly, Ed wrote:
Hey AppleScript list,
I have an efficiency question. I have a biggish file >(50,000
words, 700,00 chars, 1,000 lines)
I need to get every line that contain specific strings.
For example, if this were the handler call:
my GetLinesFromFile ({"25307", "29375", "30099", "31331",
"32167"}, filePath)
the result should be a list of 5 items where each item was a line
of text from the file containing the corresponding string.
I am currently doing this by reading the file, getting the offset
of each string, finding the line number from its position in the
text and extracting the line using text item delimiters.
There has to be a better/faster way!
Any vanilla AppleScript* suggestions?
Ed
* By 'vanilla AppleScript' I mean a standard OS X install with no
third party extensions or applications.
Ed,
Here is my suggestion:
<script>
set itemstofind to {"25307", "29375", "30099", "31331", "32167"}
set filepath to POSIX path of (("" & (path to desktop)) & "test.rtf")
my GetLinesFromFile(itemstofind, filepath, false, true)
on GetLinesFromFile(itemstofind, filepath, Ireverse, linenumber)
set gr to "grep "
if Ireverse then set gr to gr & "-v " -- want to find lines not
containing the pattern
if linenumber then set gr to gr & "-n " -- want to know line numbers
set found to {}
repeat with j in itemstofind
set end of found to (do shell script gr & j & " " & filepath) &
return
end repeat
return found
end GetLinesFromFile
</script>
adapt it to your taste.
deivy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden