do shell script "echo " & quoted form of theString & " | /usr/bin/grep -o -E '[sS][0-9]{1,2}E[0-9]{1,2}'"
It all works fine, but can I also use it to read a downloaded file ? Wikipedia gave me this to download files,
do shell script "curl -o wikipedia.html [url=http://www.wikipedia.org]www.wikipedia.org[/url]"
But I don’t know how to use them both.
What I actually want is to download the file (like the second script), and then let grep display the searchstring. But I don’t know how to use it…
Oh, the file has to be saved, not only read. (curl -o)
Instead of using echo to send data to grep, use input redirection (“<”) to arrange for grep to read from the saved file.
If you end up having multiple saved files that you want to search all at once, you may want to investigate naming the files as grep arguments and using “-h” to suppress the filename that grep shows before matches when searching multiple files.
set curlSavePath to "wikipedia.html"
set curlURL to "[url=http://www.wikipedia.org]www.wikipedia.org[/url]"
set curlCmd to "curl -o " & quoted form of curlSavePath & " " & quoted form of curlURL
set grepCmd to "/usr/bin/grep -o -E '[sS][0-9]{1,2}E[0-9]{1,2}' <" & quoted form of curlSavePath
(* PICK ONE VARIATION *)
-- If you need to fetch and grep separately:
do shell script curlCmd
-- do other stuff
do shell script grepCmd
(* OR *)
-- If you always do the grep immediately after the fetch (and always fetch immediately before the grep):
do shell script curlCmd & " ; " & grepCmd
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)
Spaces are not the only character that need to be quoted for the shell.
Others include asterisk, question mark, square brackets, curly braces, parentheses, semicolon, ampersand, greater than, less than, backslash, dollar sign, (leading) tilde, single quotes, double quotes, back quotes, and pipe (vertical bar).
Not all of these are strictly valid in URLs (they should be URI encoded (“percent escaped”)), but I think some of them are valid (at least question mark and ampersand, which are often used to supply request parameters).
Unless you specifically intend to have the shell apply special interpretation to all of the characters in a string value, it is best to quote the string value before using it in the shell.
Since the Wikipedia URL you gave did not seem likely to be the actual URL that you were going to use, I thought it would be best to show how to properly quote a string value for the shell.
You should also be aware that curl might require additional quoting/escaping (at least for square brackets and curly brackets; unless you use –globoff). Such quoting would be done as URI encoding before applying quoted form of.