• 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: Christopher Stone <email@hidden>
  • Date: Tue, 26 Jul 2011 03:56:13 -0500

On Jul 25, 2011, at 16:49, Zavatone, Alex wrote:
It turns out that all these routines are fast enough.  It's the looping through 2500 lines of source that is taking all the time.
______________________________________________________________________

Hey Alex,

Is there a particular reason you're looping through the lines?  Why not process the whole file or text literal en mass?

------------------------------------------------------------------------------------------------
on trimLeadingWhiteSpace(str)
set cmd to "sed -nE 's/^[ ]*//p'<<<" & quoted form of str
do shell script cmd
return result
end trimLeadingWhiteSpace

set theFile to alias "Thor:Users:chris:Downloads:test_strip.txt"
set str to "    Some Text 1
    Some Text 2
    Some Text 3
    Some Text 4
    Some Text 5
    Some Text 6

     

     

     




Some Text 7
"

trimLeadingWhiteSpace(str)

------------------------------------------------------------------------------------------------

This runs in 0.0427 Seconds on my machine when processing a 5000 line test file.

set theFile to alias "Thor:Users:chris:Downloads:test_strip.txt"
set str to read theFile as text
trimLeadingWhiteSpace(str)

------------------------------------------------------------------------------------------------

This script using the Satimage.osax runs in less than 5 seconds on the same file.

------------------------------------------------------------------------------------------------
# Change Handler -- Satimage.osax                                    MODIFIED 2010-09-25 : 02:44
------------------------------------------------------------------------------------------------
on cng(findText, changeText, textSource)
change findText into changeText in textSource ¬
with regexp without case sensitive
end cng
------------------------------------------------------------------------------------------------

set theFile to alias "Thor:Users:chris:Downloads:test_strip.txt"
set theText to paragraphs of (read theFile)

set newText to {}

repeat with i in theText
set end of newText to cng("^[\\t ]+", "", i)
end repeat

set AppleScript's text item delimiters to return
set newText to newText as text

------------------------------------------------------------------------------------------------

Note that I'd never loop through 5000 paragraphs unless there was a very good reason.

The Satimage.osax will operate on literal text, and it will also take a file as input to edit in place.

------------------------------------------------------------------------------------------------
# Change Handler -- Satimage.osax                                    MODIFIED 2010-09-25 : 12:44
------------------------------------------------------------------------------------------------
on cng(findText, changeText, textSource)
change findText into changeText in textSource ¬
with regexp without case sensitive
end cng
------------------------------------------------------------------------------------------------

This edits the 5000 line file in place in 0.025 Seconds:

set theFile to alias "Thor:Users:chris:Downloads:test_strip.txt"
cng("^[\\t ]+", "", theFile)

------------------------------------------------------------------------------------------------

This runs in just about the same time:

set theFile to alias "Thor:Users:chris:Downloads:test_strip.txt"
set theFileText to read theFile
set theFileText to cng("^[\\t ]+", "", theFileText)

------------------------------------------------------------------------------------------------

Here's you basic handler building on the 'cng' handler.

on trimLeadingWhiteSpace(inputSource)
cng("^[\\t ]+", "", inputSource)
return result
end trimLeadingWhiteSpace

------------------------------------------------------------------------------------------------

Mark's shell scripts work just fine on the whole literal text, although I had to adjust his sed syntax a little bit.  (Again the 5000 line test file.)

on stripLeadingWhiteSpaceRuby(str)
do shell script "ruby -pe '$_.lstrip!' <<<" & (quoted form of str)
end stripLeadingWhiteSpaceRuby
on stripLeadingWhiteSpacePerl(str)
do shell script "perl -pe 's/^\\s+//' <<<" & (quoted form of str)
end stripLeadingWhiteSpacePerl
on stripLeadingWhiteSpaceSed(str)
do shell script "sed -nE 's/^[ ]+//p' <<<" & (quoted form of str)
end stripLeadingWhiteSpaceSed

set str to read alias "Thor:Users:chris:Downloads:test_strip.txt"

stripLeadingWhiteSpaceRuby(str) --> 0.045 
stripLeadingWhiteSpacePerl(str)   --> 0.045
stripLeadingWhiteSpaceSed(str)   --> 0.043

------------------------------------------------------------------------------------------------

Note that I'm considering whitespace to be spaces and tabs and ignoring linefeeds and carriage returns.

Easy enough to strip out empty lines as well.

--
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

  • Follow-Ups:
    • Re: Trim space chars from start of line
      • From: Christopher Stone <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>)
 >Re: Trim space chars from start of line (From: "Mark J. Reed" <email@hidden>)
 >Re: Trim space chars from start of line (From: "Zavatone, Alex" <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