grep search within a string (the Abominable Grep Man)

Hi I’m having difficulty getting Grep to return the right text I presume it’s to do with the delimeters you can set in Grep but currently I’m stumped?
It’s loading all available contacts from address book and then searching them by phone number (nummerbr3) and should return the ID
for that number.


set nummerbr3 to "07985715543"
--set nummerbr2 to nummerbr2
set phn to nummerbr3
--repeat while phn is ""
--set phn to the text returned of ¬
--(display dialog "Phone number:" default answer phn with icon note)
--end repeat
tell application "Address Book"
	set Record_show to {}
	repeat with loopPerson in people
		
		
		set PhoneNumsCount to (count every phone of loopPerson)
		
		repeat with i from 1 to PhoneNumsCount
			set phoneNum to (get value of phone 1 of loopPerson)
			set thePersonID to the id of loopPerson
			--ignoring white space
			--set phoneNums to phone i of loopPerson
			--set mylist to every person
			set mylist to {phoneNum, thePersonID}
			
			exit repeat
			
			--end ignoring
		end repeat
		
		--	end repeat
		set end of Record_show to (mylist & return & return)
	end repeat
	
end tell


set AppleScript's text item delimiters to {space}
set numext to text items of (Record_show)
set StringVariableX to phn
set numext to (do shell script "echo \"" & Record_show & "\" | grep  \"" & StringVariableX & "\"") as text
--set AppleScript's text item delimiters to {""}

--set AppleScript's text item delimiters to {","} -- quotes escaped
set foundid2 to text item 2 of (numext)
set AppleScript's text item delimiters to {""}
log foundid2
set found to false
--repeat with nummerbr3 in mylist
if foundid2 is not {} then
	set found to true
	--exit repeat
end if
--end repeat
if found then
	activate
	open location "addressbook://" & foundid2 & "?edit"
	
else
	--tell application "Address Book"
	activate
	set theReply to display dialog nummerbr3 & " is not present in your Address Book. Would you like to create a new card for this number?" buttons {"No", "Yes"} default button 2 giving up after 10
	
	if the button returned of theReply is "No" then
	end if
	
	if the button returned of theReply is "Yes" then
		tell application "Address Book"
			set thePerson to make new person
			make new phone with properties {value:nummerbr3} at end of phones of thePerson
			save addressbook
			--global thePersonID
			set thePersonID to the id of thePerson
			--globalValueHandler()
		end tell
	end if
end if
try
	--open location "addressbook://" & thePersonID & "?edit"
end try
log Record_show
--end tell
end

Currently the codes returns this


tell application “Address Book”
count every person
10
count every phone of item 1 of every person
1
get value of phone 1 of item 1 of every person
“07552307693”
get id of item 1 of every person
“39DD3C82-B922-4043-A724-940BD90C5985:ABPerson”
count every phone of item 2 of every person
1
get value of phone 1 of item 2 of every person
“07595296768”
get id of item 2 of every person
“4514B426-FF6E-4A7E-9E3A-6B60C17DEA8B:ABPerson”
count every phone of item 3 of every person
1
get value of phone 1 of item 3 of every person
“01652660754”
get id of item 3 of every person
“5C7B3C05-699F-4B72-90F1-E8152CB1814B:ABPerson”
count every phone of item 4 of every person
1
get value of phone 1 of item 4 of every person
“01469533084”
get id of item 4 of every person
“526E592C-49FD-4329-B6E5-3C3C0BB932BC:ABPerson”
count every phone of item 5 of every person
1
get value of phone 1 of item 5 of every person
“07955838012”
get id of item 5 of every person
“5B0E687F-826A-40FA-8ADC-BF7F43C6C474:ABPerson”
count every phone of item 6 of every person
1
get value of phone 1 of item 6 of every person
“07835264231”
get id of item 6 of every person
“91CF1FBB-AEB2-41B3-AC56-A40EBEAC4B0E:ABPerson”
count every phone of item 7 of every person
1
get value of phone 1 of item 7 of every person
“07934993610”
get id of item 7 of every person
“A511E34B-2BC8-49CA-BB14-1A0BEFBA0E0A:ABPerson”
count every phone of item 8 of every person
1
get value of phone 1 of item 8 of every person
“07985715543”
get id of item 8 of every person
“08651E5C-6B1E-446B-8F9D-CE5D59BB3C46:ABPerson”
count every phone of item 9 of every person
1
get value of phone 1 of item 9 of every person
“07529954868”
get id of item 9 of every person
“C2AB343A-F705-40B0-B0B9-D8C4868AC6E3:ABPerson”
count every phone of item 10 of every person
1
get value of phone 1 of item 10 of every person
“1112223333”
get id of item 10 of every person
“EBDE19E9-FEEF-45C0-9379-756EBC3142AB:ABPerson”
end tell
tell current application
do shell script "echo "07552307693 39DD3C82-B922-4043-A724-940BD90C5985:ABPerson

07595296768 4514B426-FF6E-4A7E-9E3A-6B60C17DEA8B:ABPerson

01652660754 5C7B3C05-699F-4B72-90F1-E8152CB1814B:ABPerson

01469533084 526E592C-49FD-4329-B6E5-3C3C0BB932BC:ABPerson

07955838012 5B0E687F-826A-40FA-8ADC-BF7F43C6C474:ABPerson

07835264231 91CF1FBB-AEB2-41B3-AC56-A40EBEAC4B0E:ABPerson

07934993610 A511E34B-2BC8-49CA-BB14-1A0BEFBA0E0A:ABPerson

07985715543 08651E5C-6B1E-446B-8F9D-CE5D59BB3C46:ABPerson

07529954868 C2AB343A-F705-40B0-B0B9-D8C4868AC6E3:ABPerson

1112223333 EBDE19E9-FEEF-45C0-9379-756EBC3142AB:ABPerson

[b]" | grep "07985715543""
"07552307693 39DD3C82-B922-4043-A724-940BD90C5985:ABPerson

07595296768 4514B426-FF6E-4A7E-9E3A-6B60C17DEA8B:ABPerson

01652660754 5C7B3C05-699F-4B72-90F1-E8152CB1814B:ABPerson

01469533084 526E592C-49FD-4329-B6E5-3C3C0BB932BC:ABPerson

07955838012 5B0E687F-826A-40FA-8ADC-BF7F43C6C474:ABPerson

07835264231 91CF1FBB-AEB2-41B3-AC56-A40EBEAC4B0E:ABPerson

07934993610 A511E34B-2BC8-49CA-BB14-1A0BEFBA0E0A:ABPerson

07985715543 08651E5C-6B1E-446B-8F9D-CE5D59BB3C46:ABPerson

07529954868 C2AB343A-F705-40B0-B0B9-D8C4868AC6E3:ABPerson

1112223333 EBDE19E9-FEEF-45C0-9379-756EBC3142AB:ABPerson
"[/b]
(39DD3C82-B922-4043-A724-940BD90C5985:ABPerson)
activate
open location “addressbook://39DD3C82-B922-4043-A724-940BD90C5985:ABPerson?edit”
(*07552307693, 39DD3C82-B922-4043-A724-940BD90C5985:ABPerson,
,
, 07595296768, 4514B426-FF6E-4A7E-9E3A-6B60C17DEA8B:ABPerson,
,
, 01652660754, 5C7B3C05-699F-4B72-90F1-E8152CB1814B:ABPerson,
,
, 01469533084, 526E592C-49FD-4329-B6E5-3C3C0BB932BC:ABPerson,
,
, 07955838012, 5B0E687F-826A-40FA-8ADC-BF7F43C6C474:ABPerson,
,
, 07835264231, 91CF1FBB-AEB2-41B3-AC56-A40EBEAC4B0E:ABPerson,
,
, 07934993610, A511E34B-2BC8-49CA-BB14-1A0BEFBA0E0A:ABPerson,
,
, 07985715543, 08651E5C-6B1E-446B-8F9D-CE5D59BB3C46:ABPerson,
,
, 07529954868, C2AB343A-F705-40B0-B0B9-D8C4868AC6E3:ABPerson,
,
, 1112223333, EBDE19E9-FEEF-45C0-9379-756EBC3142AB:ABPerson,
,
*)
get end
end tell


I’m not worried about all the inaccuracy or need to streamline the script I would however expect the echo grep to return

" | grep "07985715543""
“07985715543, 08651E5C-6B1E-446B-8F9D-CE5D59BB3C46:ABPerson”

Not the current abomination.

If I set
set StringVariableX to phn
set numext to (do shell script “echo "” & numext & “" | grep "” & StringVariableX & “"”) as text

to

set StringVariableX to phn
set numext to (do shell script “echo "” & numext & “" | grep -o "” & StringVariableX & “"”) as text

it does stop the script thier and only shows

" | grep "07985715543""

Any help suggestions greatly appreciated.

Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

In short, grep only uses linefeeds (never carriage returns) as line breaks. Use linefeeds, not returns if you plan on sending a string to grep (or make some other accommodation, like using [i]tr ‘\r’ ‘\n’ in the pipeline between echo and grep).

set LF to ASCII character 10 -- predefined as linefeed in Leopard?

set strs to {¬
	"foo" & return & "bar" & return, ¬
	"foo" & LF & "bar" & LF ¬
	}
set r to {}
repeat with str in strs
	set end of r to do shell script "printf %s " & quoted form of str & " | grep foo"
end repeat
r (* -->
{"foo
bar", "foo"}
*)

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)