Greenrookie

Q: Applescript do shell grep

Hello,

 

I don't know much about shell grep and need some help. I have a filename variable that I want to grep and return the variable back to AS.

 

set filename to "Filename I need [Remove this bit].indd"

 

I would like to remove the the brackets including the contents, and the 1 white space in front of the first bracket.

 

Something like this pattern in Javascript.

 

\s\[.+\]

 

Could someone also recommend me a resource for beginners on shell grep.

 

Many thanks.

 

gr

OS X Yosemite (10.10.5)

Posted on May 25, 2016 6:44 AM

Close

Q: Applescript do shell grep

  • All replies
  • Helpful answers

Page 1 Next
  • by Tony T1,Solvedanswer

    Tony T1 Tony T1 May 25, 2016 8:42 AM in response to Greenrookie
    Level 6 (9,249 points)
    Mac OS X
    May 25, 2016 8:42 AM in response to Greenrookie

    You can try using sed instead of grep to remove the leading whitespace + brackets and anything in between:

     

    set filename to "Filename I need [Remove this bit].indd"

    set newfilename to do shell script "echo " & filename & " | sed 's/ \\[.*\\]//' "

     

    Here a resource for sed: http://www.grymoire.com/Unix/Sed.html  and one for grep: http://www.grymoire.com/Unix/Grep.html

    and here's a nice regex "cheat sheet": https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

  • by Pierre L.,

    Pierre L. Pierre L. May 25, 2016 1:36 PM in response to Tony T1
    Level 5 (4,484 points)
    May 25, 2016 1:36 PM in response to Tony T1

    Oops…

  • by Pierre L.,Helpful

    Pierre L. Pierre L. May 25, 2016 10:41 PM in response to Greenrookie
    Level 5 (4,484 points)
    May 25, 2016 10:41 PM in response to Greenrookie

    Since you're already using AppleScript, you could also use the following code:

     

    set filename to "Filename I need [Remove this bit].indd"

     

    set P1 to offset of "[" in filename

    set P2 to offset of "]" in filename

    if (P1 > 2) and (P2 > P1) then set filename to text 1 through (P1 - 2) of filename & text (P2 + 1) through -1 of filename

  • by VikingOSX,Helpful

    VikingOSX VikingOSX May 25, 2016 10:41 PM in response to Greenrookie
    Level 7 (21,056 points)
    Mac OS X
    May 25, 2016 10:41 PM in response to Greenrookie

    use framework "Foundation"

    set filename to "Filename I need [Remove this bit].indd"

     

    set str to current application's NSString's alloc()'s initWithString:filename

    set n to str's stringByReplacingOccurrencesOfString:" [Remove this bit]" withString:""

    set newfile to n as text


    Result: Filename I need.indd

  • by Greenrookie,

    Greenrookie Greenrookie May 25, 2016 10:43 PM in response to Tony T1
    Level 1 (12 points)
    Mac OS X
    May 25, 2016 10:43 PM in response to Tony T1

    Thanks everyone for the replies.

     

    Pierre – never thought you could do it this way!

     

    Viking – I have no idea how this works : )

     

    Tony – This looks about learn-able for me. I'll start looking into sed, appreciate the links.

     

     

    Thanks again Tony, Pierre, Viking

  • by VikingOSX,

    VikingOSX VikingOSX May 26, 2016 3:31 AM in response to Greenrookie
    Level 7 (21,056 points)
    Mac OS X
    May 26, 2016 3:31 AM in response to Greenrookie

    It is AppleScript/Objective-C.

     

    Basically, I initialize a string object (str) with your filename string. I then call a string replacement method (function) on that string object, and substitute the null string for the specified replacement text. The result is the desired filename.

  • by Tony T1,

    Tony T1 Tony T1 May 26, 2016 3:27 PM in response to VikingOSX
    Level 6 (9,249 points)
    Mac OS X
    May 26, 2016 3:27 PM in response to VikingOSX

    How do you use wildcards with that? (Only works as posted with: " [Remove this bit]")

  • by VikingOSX,

    VikingOSX VikingOSX May 26, 2016 8:03 PM in response to Tony T1
    Level 7 (21,056 points)
    Mac OS X
    May 26, 2016 8:03 PM in response to Tony T1

    I came up with the same sed solution that you did, and you posted first. Here is the NSRegularExpression approach using your pattern (with wildcard).


    use framework "Foundation"

     

    set filename to "Filename I need [Remove this bit].indd"

    set pattern to "\\s+\\[.*\\]"

    set replaceStr to ""


    set str to current application's NSString's alloc's initWithString:filename

    set regex to current application's NSRegularExpression's regularExpressionWithPattern:pattern options: 0 |error|: (missing value)

    set srange to current application's NSMakeRange(0, str's |length|)

    set newfile to (regex's stringByReplacingMatchesInString:str options: 0 range:srange withTemplate:replaceStr) as text


    Result: "Filename I need.indd"

  • by Greenrookie,

    Greenrookie Greenrookie May 31, 2016 7:40 AM in response to VikingOSX
    Level 1 (12 points)
    Mac OS X
    May 31, 2016 7:40 AM in response to VikingOSX

    Hello again,

     

    I have another question on the original answer – how do I pass a variable into the replacement part of the shell, but not at the end?

     

    set filename to "Filename I need [Remove this bit].indd"

    set newBit to " Plus this new bit"


    So final string reads "Filename I need Plus this new bit.indd"


    not "Filename I need.indd" Plus this new bit


    Thank you!

  • by VikingOSX,

    VikingOSX VikingOSX May 31, 2016 10:07 AM in response to Greenrookie
    Level 7 (21,056 points)
    Mac OS X
    May 31, 2016 10:07 AM in response to Greenrookie

    use framework "Foundation"

     

    set filename to "Filename I need [Remove this bit].indd"

    set pattern to "\\s+\\[.*\\]"

    set replaceStr to " Plus this new bit"

     

    set str to current application's NSString's alloc's initWithString:filename

    set regex to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|: (missing value)

    set srange to current application's NSMakeRange(0, str's |length|)

    set newfile to (regex's stringByReplacingMatchesInString:str options:0 range:srange withTemplate:replaceStr) as text


    Result: Filename I need Plus this new bit.indd

  • by Greenrookie,

    Greenrookie Greenrookie Jun 1, 2016 3:14 AM in response to VikingOSX
    Level 1 (12 points)
    Mac OS X
    Jun 1, 2016 3:14 AM in response to VikingOSX

    Thanks again Viking!

  • by Tony T1,

    Tony T1 Tony T1 Jun 5, 2016 6:04 AM in response to Greenrookie
    Level 6 (9,249 points)
    Mac OS X
    Jun 5, 2016 6:04 AM in response to Greenrookie

    Also can be done with sed:

     

    sed 's/ \\[.*\\]//' replaces the string with nothing (//).  to replace with something: (/ Plus this new bit/)

     

    In Applescript:

         set filename to "Filename I need [Remove this bit].indd"

         set newBit to " Plus this new bit"

         set newfilename to do shell script "echo " & filename & " | sed 's/ \\[.*\\]/" & newBit & "/' "

  • by Greenrookie,

    Greenrookie Greenrookie Jun 5, 2016 6:17 AM in response to Tony T1
    Level 1 (12 points)
    Mac OS X
    Jun 5, 2016 6:17 AM in response to Tony T1

    Thanks Tony, that was what I was looking for originally.

     

    I also came stuck with case insensitive problems, then found a bit of code on SE that I adapted.

     

    set newfilename to do shell script "echo " & filename & " | perl -C -Mutf8 -pe 's/" & OldVariable & "/" & NewVariable & "/gi'"

     

     

     

    One more question. Is it possible to pass an expression along with a variable at the same time?

     

    Something like this:

     

    sed 's/ .* & OldVariable & "/" & newBit & "/' "

     

    Maybe a tad too crazy?

     

    Tx

  • by Tony T1,

    Tony T1 Tony T1 Jun 5, 2016 6:38 AM in response to Greenrookie
    Level 6 (9,249 points)
    Mac OS X
    Jun 5, 2016 6:38 AM in response to Greenrookie

    Like this?


    set filename to "Filename I need [Remove this bit].indd"

    set OldVariable to "Remove this bit"

    set newBit to " Plus this new bit"

    set newfilename to do shell script "echo " & filename & " | sed 's/ \\[" & OldVariable & "\\]/" & newBit & "/' "

     

     

    Result:

    "Filename I need Plus this new bit.indd"

Page 1 Next