MACSCRPT Archives

September 2009

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"Mark J. Reed" <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Wed, 9 Sep 2009 13:14:17 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (33 lines)
On Wed, Sep 9, 2009 at 12:46 PM, Stockly, Ed<[log in to unmask]> wrote:
> Do shell script "grep ^" & foo & "\|" & myFile

Hm. That doesn't compile for me.  You need to double the backslash.

Or lose it entirely.  Are you trying to match a literal pipe character
in the string?  If so, you want it without the backslash for grep(1).
With the backslash it means "or" and you're matching any line that
contains the empty string, which is all of them.

You can use that feature to your advantage to solve your problem, however:

grep '^\(foo\|bar\|zoo\)|'  will match lines starting with 'foo|',
'bar|', or 'zoo|'.


Something like this from AS:

set lookForThese to { "foo", "bar", "zoo" }
set text item delimiters to "\\|"
set thePattern to "^\\(" & (lookForThese as text) & "\\)|"
do shell script "grep " & quoted form of thePattern & " myFile"

> Takes a while, but it works.

If grep is slow, egrep might be faster.  You have to swap the
backslashes, though:
grep '^\(foo\|bar\|zoo\)|' becomes egrep '^(foo|bar|zoo)\|'


-- 
Mark J. Reed <[log in to unmask]>

ATOM RSS1 RSS2