why doesn't this curl and grep command work?

If I run this script to get the title of a movie from the IMDB movie database it works.

set movie_title to do shell script "curl http://www.imdb.com/title/tt0322259/ | grep -i \"<title>\""

But I need to get other data from the curl results so I want to store the curl results in a variable and then run grep like the following… but it doesn’t work. Can anyone help me fix this?

set movie_page to do shell script "curl [url=http://www.imdb.com/title/tt0322259/]http://www.imdb.com/title/tt0322259/"[/url]
set movie_title to do shell script "echo " & quoted form of movie_page & " | grep -i \"<title>\""

Hmm… odd I am sure I have never run into that before.

It looks like the newline is being stripped some how when AS stores the text in the variable. Something to do with way it coerces it. and maybe the line ending on the page.

When you run the first script the problem is not encounter because grep is picking up the text from the page straight away before AS.

To demostrate I have put the newline back in (\n) in set this_item to paragraph i of movie_page & "
"

set movie_page to do shell script "curl [url=http://www.imdb.com/title/tt0322259/]http://www.imdb.com/title/tt0322259/"[/url]
set movie_pageList to {""}
repeat with i from 1 to number of paragraphs in movie_page
	set this_item to paragraph i of movie_page & "
"
	
	set item 1 of movie_pageList to item 1 of movie_pageList & this_item
	
end repeat

set movie_title to do shell script "echo " & quoted form of item 1 of movie_pageList & " | grep -i \"<title>\" "

I figured it out… this works. I had to change the line endings. Ahhhh, I see Mark found a solution too. Thanks Mark.

set movie_page to do shell script "curl [url=http://www.imdb.com/title/tt0322259/]http://www.imdb.com/title/tt0322259/"[/url]
set text item delimiters to return
set a to text items of movie_page
set text item delimiters to (ASCII character 10)
set movie_page to a as string
set text item delimiters to ""
set movie_title to do shell script "echo " & quoted form of movie_page & " | grep -i \"<title>\""

FYI, since both solutions work I didn’t know which to use… so I ran a quick speed test to see which code ran faster (using the GetMilliSec speed test). For 50 iterations here’s the results:

my code: 3.376 secs
Mark’s code: 24.94 secs

It seems my code is about 7.4 times faster so I’ll be using that. I appreciate your help Mark, Thanks.

Because do shell script changes them by default.

[I just saw that Jacques has already posted, but I’ll continue. :)]

Try this instead:

set movie_page to do shell script "curl [url=http://www.imdb.com/title/tt0322259/]http://www.imdb.com/title/tt0322259/"[/url] without altering line endings
set movie_title to do shell script "echo " & quoted form of movie_page & " | grep -i '<title>'"

That’s really sweet Jacques, thanks. Oh, and Bruce too, thanks!

I didn’t know about the automatic line-endings transformation. I just checked the standard additions dictionary and there it is for the do shell script command… altering line endings boolean] : change all line endings to Mac-style and trim a trailing one (default true). I guess that’s the one I’ll be using!

why not set the text item delimiter to “”, get the 2nd item, then set the text item delimiter to “” and not use the grep at all?