• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How do I filter out one character?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I filter out one character?


  • Subject: Re: How do I filter out one character?
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 30 Oct 2018 18:30:59 -0500

On 10/29/2018, at 14:53, Jeffrey Madson <email@hidden
<mailto:email@hidden>> wrote:
> The problem is that there is an extra character I need to filter out the
> third character in every case so it should look like this: CA2673AC638520.


Hey Jeffrey,

When asking for help like this it's a good idea to provide a minimum possible
working code sample.

You're also asking a shell scripting question in an AppleScript forum, and
while there's nothing wrong with that it does reduce the likelihood that you'll
get useful help.

Especially when the shell script is not presented as an embedded part of an
AppleScript.

You want to always remove the 3rd character and do a transformation similar to
this?

CA02673AC638520 --> CA2673AC638520

Since you're working in the shell let's stay in the shell.

num=CA02673AC638520

# You're already familiar with `cut`, but perhaps you need to study it a little
more:
echo $num | cut -c 1-2,4-

# Apple's `sed` is (quite ridiculously) 13+ years old, but it will do the job:
echo $num | sed -E 's!^(..).(.+)!\1\2!'

** Installing GNU sed with MacPorts or Homebrew gives you a much more modern
and powerful version.

# Perl gives you a more modern, immensely more powerful regular expression
engine.
echo $num | perl -lne 's!^(.{1,2}).(.{4,})!$1$2!; print'


Here's your most basic way of doing this in AppleScript with the output of a
shell script:

set shCMD to "echo 'CA02673AC638520'"
set shellOutput to do shell script shCMD

set valueWithStrippedChar to {text 1 thru 2, text 4 thru -1} of shellOutput --
produces an AppleScript list object.

--> {"CA", "2673AC638520"}

set valueWithStrippedChar to valueWithStrippedChar as text -- concatenates the
list.

--> "CA2673AC638520"


Of course these two lines can be distilled into a one-liner

set shCMD to "echo 'CA02673AC638520'"
set shellOutput to do shell script shCMD

# It's good practice to make sure tids are the default – if not you might get
unexpected results.
set AppleScript's text item delimiters to ""
set valueWithStrippedChar to (get {text 1 thru 2, text 4 thru -1} of
shellOutput) as text


--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >How do I filter out one character? (From: Jeffrey Madson <email@hidden>)

  • Prev by Date: How do I filter out one character? continued....
  • Next by Date: Re: How do I filter out one character? continued....
  • Previous by thread: Re: How do I filter out one character?
  • Next by thread: Can anyone inspect menu elements by using Accessibility Inspector v5 ?
  • Index(es):
    • Date
    • Thread