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: "Nigel Garvey" <email@hidden>
- Date: Wed, 21 Dec 2005 17:01:49 +0000
"Stockey, Ed" wrote on Tue, 20 Dec 2005 17:05:39 -0800:
>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 take it from this that you're only expecting one instance each of these
substrings in the text file.
>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.
You can also do it the other way round: set the TIDs to the string, count
the paragraphs of the "first text item" of the file text, and extract the
that-numbered paragraph. It doesn't use any OSAX calls during the repeat;
but not having seen your original code, I can't tell if it's a great
improvement or not:
on GetLinesFromFile(searchList, filePath, textClass)
set theText to (read file filePath as textClass from 1)
set matchingLines to {}
set astid to AppleScript's text item delimiters
repeat with i from 1 to (count searchList)
set AppleScript's text item delimiters to item i of searchList
if ((count theText's text items) > 1) then
set end of matchingLines to paragraph (count paragraphs of
theText's first text item) of theText
else
set end of matchingLines to missing value
end if
end repeat
set AppleScript's text item delimiters to astid
return matchingLines
end GetLinesFromFile
set searchList to {"25307", "29375", "30099", "31331", "32167"}
set filePath to (path to At Ease documents folder as Unicode text) & "A
text file.txt"
set fileTextClass to string
GetLinesFromFile(searchList, filePath, fileTextClass)
NG
_______________________________________________
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