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
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/
Posted on May 25, 2016 8:42 AM