• 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: text item delimiter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: text item delimiter


  • Subject: Re: text item delimiter
  • From: "Mark J. Reed" <email@hidden>
  • Date: Fri, 29 Apr 2005 11:08:27 -0400



On 4/29/05, Rob Stott <email@hidden> wrote:

On 29 Apr 2005, at 15:25, Ruby Madraswala wrote:

 Is this doable using text item delimiters, I tried it's not working. All I want to do is search a text file replace all occurrence of 1 to a, 2 to b.........................

I wouldn't do it using TIDs. Here's how I'd do it ...

tell application "Finder"
    set theFile to quoted form of (POSIX path of (choose file))
end tell

set theResult to do shell script "cat " & theFile & " | sed 'y/abcdefghi/123456789/' | sed 's/j/10/g' | sed 's/k/11/g' | sed 's/l/12/g' | sed 's/m/13/g'   | sed 's/n/14/g'  | sed 's/o/15/g'  | sed 's/p/16/g'   | sed 's/q/17/g'  | sed 's/r/18/g'  | sed 's/s/19/g'   | sed 's/t/20/g'  | sed 's/u/21/g'  | sed 's/v/22/g'   | sed 's/w/23/g'  | sed 's/x/24/g'  | sed 's/y/25/g'   | sed 's/z/26/g'"


Ow. 



...theres almost certainly a better way that doesn't involve that many 'sed's for multiple digits but I'm not sure how. Can anyone put me straight on that one?


Sure.  A few points.

1. "cat file | command" is always a waste of a process.  You can just do "<file command", no "cat" required.

2. A single sed will take as many substitutions as you want: "sed -e s/1/a/g -e s/2/b/g ..."  If the from and to strings are more than one character each, that's what you would do.

3. But if the from and to are really individual digits and letters, especially if they're in sequence, the "tr" command is a better choice than sed.    "tr abcd 1234" will do the trick; you can also abbreviate sequences and rewrite it as "tr [a-d] [1-4]".



r.



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (




--
Mark J. Reed <
email@hidden>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: text item delimiter
      • From: Rob Stott <email@hidden>
References: 
 >text item delimiter (From: "Ruby Madraswala" <email@hidden>)
 >Re: text item delimiter (From: Rob Stott <email@hidden>)

  • Prev by Date: Re: Exiting from an Applescript
  • Next by Date: Re: text item delimiter
  • Previous by thread: Re: text item delimiter
  • Next by thread: Re: text item delimiter
  • Index(es):
    • Date
    • Thread