How do you retrieve a line that contains search term?

This is probably very basic, but tough for this newbie. I would like to learn a way for Applescript to search through some text for a name and return the entire line (name, tab, number). This forms part of a much larger script.

Example (the lists exist in this form with tabs and line returns, but it could be formatted differently if this would help):

Joe 37.5
Bill 22.4
Larry 17.5

I’d like to search for “Bill” and have it come back with “Bill 22.4” (name, tab, number ”without the return)

Thanks for any help.

Mike

I’ve recreated the text with tabs and returns because tabs don’t show on the web page. This works if there’s only one instance of ‘Bill’. If there could be several, a different approach is required below.


set T to "Joe" & tab & "37.5" & return & "Bill" & tab & "22.4" & return & "Larry" & tab & "17.5"

set S to "Bill"

repeat with p in paragraphs of T
	if contents of p contains S then
		set F to contents of p
		exit repeat -- stop looking
	end if
end repeat
F --> Bill[with tab here]22.4

For multiples (which would be missed in the first approach):


set T to "Joe" & tab & "37.5" & return & "Bill" & tab & "22.4" & return & "Larry" & tab & "17.5" & return & "Bill" & tab & "43.2"

set S to "Bill"
set F to {}
repeat with p in paragraphs of T
	if contents of p contains S then set end of F to contents of p -- keep looking
end repeat
--> returns a list of every encounter, two in this case.

Thanks, that’s great! In this case, there will always be only one of each name to be found.

I really appreciate the help.

Mike