Breaking down really big string

I have a big string. 50,000+ lines. I want to break it down into smaller chucks, like 500 lines a piece.

The following script takes too long. Please show me a better way :slight_smile: (as you can see, this string will be logged, but I’m reaching the size limit of a log entry. If there’s a more elegant way to log something that keeps me from having to do this then that would be great too.)


on logByNumLines(aString, LinesPerLog, logFile)
	set lineCount to count of paragraphs of aString
	set j to 1
	set aLog to "" & return
	repeat with i from 1 to lineCount
		if j ≤ LinesPerLog then
			set aLog to (aLog & (paragraph i of aString) as text) & return
			set j to j + 1
		else
			do shell script ¬
				"echo `date '+%Y-%m-%d %T: '`\"" & aLog & ¬
				"\" >> $HOME/Library/Logs/" & logFile & ".log"
			set aLog to "" & return
			set j to 1
		end if
	end repeat
	if j > 1 then
		do shell script ¬
			"echo `date '+%Y-%m-%d %T: '`\"" & aLog & ¬
			"\" >> $HOME/Library/Logs/" & logFile & ".log"
	end if
end logByNumLines

to split a string in chunks of 500 it’s better to use TID and getting text items 1 thru 500, text items 501 thru 1000, text items 1001 thru 1500 etc… it will make your script a lot faster.

Will that preserve new line characters?

If you’re using text item delimiters it’s even better than using paragraphs. It’s better because linefeed, return and extensible returns are all included in paragraphs and won’t be preserved but all be changed in your current code.

edit: here an example

set theText to "this is line 1
this is line 2
this is line 3
this is line 4
this is line 5"

set AppleScript's text item delimiters to linefeed
set newText to text items 2 thru 4 of theText as string
set AppleScript's text item delimiters to ""
return newText

Hi.

The proper way to extract a chunk of so many paragraphs from a text is with ‘text from paragraph x to paragraph y of theText’ or ‘text (paragraph x) thru (paragraph y) of theText’. Similarly with text items.

scriptim’s handler could thus look something like this:


on logByNumLines(aString, LinesPerLog, logFile)
	set lineCount to (count paragraphs of aString)
	repeat with i from 1 to lineCount by LinesPerLog
		set j to i + LinesPerLog - 1
		if (j > lineCount) then set j to lineCount
		set aLog to return & text from paragraph i to paragraph j of aString
		do shell script ¬
			"echo `date '+%Y-%m-%d %T: '`\"" & aLog & ¬
			"\" >> $HOME/Library/Logs/" & logFile & ".log"
	end repeat
end logByNumLines

I suspect that the limit is actually the length of text which can be handled by a single ‘do shell script’ command, not a limit on log entries per se. If that’s all it is, a single write using the File Read/Write commands should be a lot faster than 100 ‘do shell script’ calls.

Apparently I was overloading the echo command so it failed and returned a non-zero status which threw a wrench in the script.

These solutions are just what I was looking for!

Thanks guys!