Grep - finding alternative words / patterns

If I’m looking for lines that contain xxxx what’s below works fine.


set target_url to "http://www.mywebsite.com"

do shell script "/usr/bin/curl " & target_url & "| grep 'xxxx' | textutil  -stdin -stdout -format html -convert txt -encoding UTF-8"

However, what I’ld like to do is get lines that contain xxxx or yyyy.

I’ve searched and experimented, but to no avail. I expect I’m getting the syntax wrong.

Any help would be appreciated.

Thanks

Hi,

if you don’t mind using egrep:

Returns

Regards

Grep uses “|” for its “or operator”. Since a backslash has a special meaning to applescript, in order to send it through to the shell you must escape it like this…

set theText to "xxxx
yyyy
zzzz"

do shell script "echo " & quoted form of theText & " | grep " & quoted form of "xxx\\|yyy"

That simple, eh! Thank you.

Whilst people are reading can I ask another syntax question.

I would expect this to return any line with at least four numbers in it.

| grep ‘[0-9]{MIN,4}’ |

These exist in my source file, but “” is returned.

What’s my mistake?

Thanks

You need to escape the curly brackets. Also, there seems to be an error in your {min,max}-expression.

{4} → exactly four
{3,4} → at least three, no more than four
{3,} → at least three

In your case, using the exact number (as used below) should be sufficient.

E.g.

set  txt to "1234689
y123
z1234"

do shell script "/bin/echo " & quoted form of txt & " |grep  '[0-9]\\{4\\}'" 

Returns

Regards

Very helpful - thanks