• 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: Trim space chars from start of line
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trim space chars from start of line


  • Subject: Re: Trim space chars from start of line
  • From: "Mark J. Reed" <email@hidden>
  • Date: Mon, 25 Jul 2011 17:29:04 -0400

On Mon, Jul 25, 2011 at 5:20 PM, Mark J. Reed <email@hidden> wrote:

So much unnecessary work!  Our old friend "quoted form of" will take care of all the apostrophe-munging if you don't try to turn the string into a Ruby source code literal in addition to making it through the shell:

on stripLeadingWhiteSpace(str)
   do shell script "ruby -pe '$_.lstrip!' <<<" & (quoted form of str)
end stripLeadingWhiteSpace

Of course, Ruby, at least in its pre-1.9 C-based form, is not known for its speed.   Perl is probably faster:

on stripLeadingWhiteSpace(str)
   do shell script "perl -pe 's/^\\s+//' <<<" & (quoted form of str)
end stripLeadingWhiteSpace

Or even good old-fashioned sed:

on stripLeadingWhiteSpace(str)
   do shell script "sed 's/^  *//' <<<" & (quoted form of str)
end stripLeadingWhiteSpace

For that matter, the shell itself automatically strips leading whitespace from input lines read with "read", though I wouldn't necessarily trust it to pass everything else through intact:

on stripLeadingWhiteSpace(str)
   do shell script "(read line; echo \"$line\") <<<" & (quoted form of str)
end stripLeadingWhiteSpace


-- 
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:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Trim space chars from start of line
      • From: "Zavatone, Alex" <email@hidden>
References: 
 >Trim space chars from start of line (From: "Zavatone, Alex" <email@hidden>)
 >Re: Trim space chars from start of line (From: KOENIG Yvan <email@hidden>)
 >Re: Trim space chars from start of line (From: "Zavatone, Alex" <email@hidden>)
 >Re: Trim space chars from start of line (From: Stan Cleveland <email@hidden>)
 >Re: Trim space chars from start of line (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: Trim space chars from start of line
  • Next by Date: Re: Trim space chars from start of line
  • Previous by thread: Re: Trim space chars from start of line
  • Next by thread: Re: Trim space chars from start of line
  • Index(es):
    • Date
    • Thread