Tuesday, January 21, 2020

#1 2020-01-08 07:02:05 pm

kenexzo
Member
Registered: 2019-12-15
Posts: 14

How To Delete Characters Inside Brackets [ ]

Basically, I have the following string:

@junior_cat23, [ID]-[E9B-Z8X-H1V-722]


I want the script to remove only this part:

, [ID]-[E9B-Z8X-H1V-722]


So that I am left with:

@junior_cat23


I was provided with the following Applescript which does the job:

Applescript:

set theString to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

But the problem is that it only removes items that are inside the brackets when they are located on the right hand side of the string.

If I have the following string:

[ID]-[E9B-Z8X-H1V-722] this is a test @junior_cat23 @james_cat24


It will delete the entire string.

Applescript:

set theString to "[ID]-[E9B-Z8X-H1V-722] this is a test @junior_cat23 @james_cat24"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

If I have the following:

this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24


It will only return:

this is a test


Applescript:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'<<<" & theString's quoted form

I just want to remove the characters in side of the brackets no matter where they are located in the string (wether they are on the left, right or middle) and keep the others.

I did find a shell script which works in macOS Terminal but I am having issues integrating it into an Applescript

echo "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24
"|sed 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'


I tried adapting it to my script editor with the "do shell script" command and I get the following error:

Expected “"” but found unknown token.


Here is the script:

Applescript:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'" & theString's quoted form

Any help is appreciated. Thanks!

Last edited by kenexzo (2020-01-09 05:19:11 am)


Filed under: terminal, applescript, shell, bash

Offline

 

#2 2020-01-08 07:54:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: How To Delete Characters Inside Brackets [ ]

Here's an alternative approach:

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theText to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]
this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"

set theText to current application's NSString's stringWithString:theText
return (theText's stringByReplacingOccurrencesOfString:",? \\[[^]]+?]-\\[[^]]+?]" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, theText's |length|()}) as text


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-01-08 08:05:05 pm

kenexzo
Member
Registered: 2019-12-15
Posts: 14

Re: How To Delete Characters Inside Brackets [ ]

Thank you so much! I see it does get the result I want. But I am still curious to know why the shell script is not working with in the Script Editor. I see it works flawlessly in the macOS Terminal. Any ideas as to what the problem might be?

Works fine in Terminal:

echo "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24
"|sed 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'


Does not work in Applescript Editor:

Applescript:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'" & theString's quoted form

Offline

 

#4 2020-01-08 09:10:42 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: How To Delete Characters Inside Brackets [ ]

That sort of thing is usually a quoting or escaping problem.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-01-09 04:36:57 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5147

Re: How To Delete Characters Inside Brackets [ ]

kenexzo wrote:

Does not work in Applescript Editor:

Applescript:

set theString to "this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24"
do shell script "sed -E -e 's/\[[^]]*\]//g' |sed -e 's|-||g' |sed -e 's/,/ /g'" & theString's quoted form


This works with all three of your examples and a pathological one:

Applescript:

set theString to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]
[ID]-[E9B-Z8X-H1V-722] this is a test @junior_cat23 @james_cat24
this is a test [ID]-[E9B-Z8X-H1V-722] @junior_cat23 @james_cat24
[ID]-[E9B-Z8X-H1V-722] this is a test [ID]-[E9B-Z8X-H1V-722][ID]-[E9B-Z8X-H1V-722] @junior_cat23, [ID]-[E9B-Z8X-H1V-722] @james_cat24[ID]-[E9B-Z8X-H1V-722]"


do shell script "sed -E 's/^\\[[^]]+\\]-\\[[^]]+\\] *|,? *\\[[^]]+\\]-\\[[^]]+\\]//g' <<<" & theString's quoted form


NG

Offline

 

#6 2020-01-09 05:12:37 am

kenexzo
Member
Registered: 2019-12-15
Posts: 14

Re: How To Delete Characters Inside Brackets [ ]

Perfect. I guess will use one of the two solutions posted instead of trying to fix the error I am getting with my original shell script. Thanks.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)