Read file and search each Paragragh

Hi all,
I have a file that contains lots of names and addresses, each name and address is on 1 line followed by a return (new paragraph). I’m trying to search each paragraph for some text and if it finds that text display the whole paragrah, this is what I have.

set myFile to "mydatabase"
set mypathtodesktop to path to desktop
set myfilepath to mypathtodesktop & myFile as string
set thedata to read file myfilepath

display dialog "enter name to search" default answer ""
set search_criteria to text returned of the result

repeat with i from 1 to the number of paragraphs of thedata
	set thispara to paragraph i of thedata
--display dialog thispara
	if thispara contains search_criteria then
		display dialog thispara
	end if
end repeat

If I display dialog I can see each paragrah but it doesn’t want to find my search_criteria. Is this something to do with thedata and the way it’s read, 't’s driving me mad :mad: Please help!

Hi,

your script works fine for me, maybe it’s a question of text encoding.
The script can be simplified a bit

set myFile to "mydatabase"
set mypathtodesktop to path to desktop
set myfilepath to mypathtodesktop & myFile as string
set thedata to paragraphs of (read file myfilepath)

set search_criteria to text returned of (display dialog "enter name to search" default answer "")

repeat with i in thedata
	--display dialog thispara
	if i contains search_criteria then
		display dialog contents of i
	end if
end repeat

Hi.

Could be. Try changing the fourth line to:

set thedata to (read file myfilepath as unicode text)

If your file contains Unicode text, this should cure the problem. If it doesn’t, I don’t know what to suggest. :confused:

Thanks for your responses guys but I still can’t get it to work. I’ve tried to simplify it buy taking out the read part and the code below works.

set myparagraph to "this is a paragraph
of text"
set thispara to paragraph 1 of myparagraph
if thispara contains "is" then
	display dialog thispara
end if

completely lost for ideas :frowning:

OK, it’s getting silly now, I’m completely confused and my head hurts. How can the script below WORK

set thedata to "name: bob 	address:Playboy Mansion
name: dave 	address:10 Downing Street
name: nik 	address:Buckingham Palace"
set thelist to every paragraph of thedata

repeat with i from 1 to the number of items in thelist
	set thisdata to item i of thelist
	if thisdata contains "nik" then
		display dialog thisdata
	end if
end repeat

but the script below not work!!!

set myFile to "mydatabase"
set mypathtodesktop to path to desktop
set myfilepath to mypathtodesktop & myFile as string
set thedata to read file myfilepath
set thelist to every paragraph of thedata

repeat with i from 1 to the number of items in thelist
	set thisdata to item i of thelist
	if thisdata contains "nik" then
		display dialog thisdata
	end if
end repeat

HELP ME PLEASE!!!

Arhh, Corrupt file. I’ve recreated the data file and it works.
Thanks for your views and comments

Hi,

I assume, that your text file is UTF-16 encoded, so try to read the file as Unicode text

set thedata to (read file myfilepath as Unicode text)