MACSCRPT Archives

December 2007

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:
"Steven D. Majewski" <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 14 Dec 2007 15:05:46 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (110 lines)
On Dec 14, 2007, at 8:27 AM, jeff donovan wrote:

> Greetings
>
> I have a regular expression I am having difficulty incorporating  
> into an apple script
>
> I have a folder with a bunch of files labeled 1234. 1235. 1236 etc,..
> so I have been using this to delete only these files but leave the  
> others alone;
>
> rm -f $( ls /Users/drfoo/scripts/HERE/Deleted\ Message | grep "[0-9] 
> \.")
>
> the folder " deleted message " does have a space in the name, i  
> can't change that. it's when i get to the digit period that apple  
> script yells at me so then I'm forced to do this
>
> do shell script "rm -f $(ls " & folderPath & aName & "/deleted\\  
> messages/ | grep [0-9]\\.)"
>
> this is not working . any hints?
>

Can't tell what's wrong without more info -- like what exactly it  
yells at you about.
But one possibility:
  There's a semantic difference between:
	grep "[0-9]\."
  and:	
	grep  [0-9]\.

  In the 2nd case (without quoting) the shell will try to expand the  
expression
before passing it on to grep. Whether it expands depends on whether  
there is
a match in the directory. If one or more files match that pattern,  
then the
pattern will get replaced with those matches. See the behavior of  
'echo' below
as an example:


$ ls
1234            1234.tmp        1235.           1236            1236.tmp
1234.           1235            1235.tmp        1236.

$ echo [0-9]
[0-9]

$ echo [0-9]*[0-9]
1234 1235 1236

$ echo "[0-9]*[0-9]"
[0-9]*[0-9]


( If the pattern in your command expands into more than one arg, then  
the first is
   going to be the pattern and the 2nd will be the input file rather  
than stdin.  )


So you probably need to quote the arg to grep.

Within a applescript string, you can often use single quotes to quote  
something to the shell.
( But note that the behavior of single and double quoted strings in  
the shell is different. )
( Simularly, when testing AS statements via osascript, I usually use  
single quotes to wrap
   the applescript statement in the shell, so that I can use double  
quotes within for applescript
   strings. )
If you have to have several levels of quoting in your applescript ->  
shell strings, then you
may have to escape some of the quotes ( and sometimes this also means  
multiple backslashes too.
It's good to replace part of the commands with 'echo' so that you can  
see what exactly is getting
thru to the shell. )

Another way to avoid nested quotes is to use 'quoted form of ... ' in  
applescript.
  '"ls " & quoted form of ( pathname & "Deleted Message" )'  is  
another way of passing your path to the shell.



Also: A better way to do what you're trying to do may be 'find',  
except that the pattern matching is not
full regex, but more like the basic shell wildcard expansion. But you  
can do something like:

   do shell script ( "find " & ( quoted form of basedir ) & " -name '* 
[0-9].*' -delete "  )

In fact, considering the difference between grep and shell matching,  
I guess the equivalent shell
pattern to your grep pattern (without any anchors) would be '* 
[0-9].*' , so why not just
'rm -f $DIRNAME/*[0-9].* '  ?  ( 'find' has the advantage that you  
could add other qualifiers
for safety, if you wanted. ) [ You might also consider 'xargs' as  
another way of joining commands. ]



-- Steve Majewski

ATOM RSS1 RSS2