• 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: Command line text substitution
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Command line text substitution


  • Subject: Re: Command line text substitution
  • From: Paul Skinner <email@hidden>
  • Date: Fri, 17 Jan 2003 07:21:11 -0500

On Thursday, January 16, 2003, at 10:19 PM, John W Baxter wrote:

At 23:56 -0500 1/10/2003, brian mather wrote:
From Paul Skinner:

Ok, I'm trying to use the right tools. What's the right CL tool for
replacing a string within a string? perl is an obvious choice. So, how
would I replace a given string in another string using do shell script?
--
Paul Skinner


How about sed?

sed 's/string1/string2/' filename


In the bash shell, it goes roughly like this:

[john@Zeus john]$a='I want AppleScript.'
[john@Zeus john]$b=${a/Apple/Pear}
[john@Zeus john]$echo $b
I want PearScript.
[john@Zeus john]$

The leftmost $ in those lines is the end of the prompt...start reading
after it.

Does that look something like the sed substitution? Yes
Is that an accident? No

Does it look something like the Perl substitution? Yes
Is *that* an accident? No


And I assume until testing that this would be faster since it doesn't spawn perl or any other process. Right?

And for the determined shorteners in the crowd:
[john@Zeus john]$a='I want AppleScript.' ; echo ${a/Apple/Pear}
I want PearScript.

As for how to do it in tcsh: I don't know and no longer care. But it
should be some small syntactic variation on the above.


Copy and pasted. Works in do shell script. That is tcsh right? The shell game is still very loose in my mind.

do shell script "a='I want AppleScript.' ; echo ${a/Apple/Pear}"
-->"I want PearScript." (unicode text)

You can have file globing wild cards in the pattern above (between the pair
of /s, that is, Apple above). ? is any one character and * is any string of
characters including empty...not the same as * in regular expressions.

--John

--
John Baxter Port Ludlow, WA, USA

Thanks John, for pulling me farther down the road. I loathed the command line for so long. Now it's becoming just another app for AppleScript to use. : )

--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Command line text substitution
      • From: John W Baxter <email@hidden>
References: 
 >re: Command line text substitution (From: John W Baxter <email@hidden>)

  • Prev by Date: Routines for review: Hex <-> String
  • Next by Date: Re: Input for Upcoming Book
  • Previous by thread: re: Command line text substitution
  • Next by thread: Re: Command line text substitution
  • Index(es):
    • Date
    • Thread