Trim n characters from each line

I’m very new at this. I want to remove the first 7 characters from each line. Simple enough but I can’t seem to find anything online. Please help.

206487 11010
206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852

Thank you!

This should work


set theText to "206487    11010
206486    11061
206485    11068
206242    10837
206241    10838
206243    10840
206251    11167
206239    88806
206240    88807
206244    88808
206245    88809
206248    88854
206249    88818
206238    88819
206250    88849
206247    88852"

set myNewText to ""
repeat with aP in paragraphs of theText
	set myNewText to myNewText & (text 8 thru -1 of aP's contents) & return
end repeat

(I am assuming you have 4 empty spaces between the repetition of the text blocks on the same line). In case it is a tab, it - most likely - still works. Otherwise let us know.

AppleScript is really simple, but not nearly as easy as some newbies think.

One problem has already been pointed out by idicore. He has to guess how many spaces or tabs there are in your data. The second problem I will point out is that it is advisable to leave the endings of the paragraphs original, and not spank the endings at random. The following script does not guess nothing:


set endindgs to {linefeed, return}

set theText to "206487 11010
206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852"

set textItems to theText's characters
set |paragraphs| to paragraphs of theText
set countParagraphs to count |paragraphs|

-- build new text
set newText to ""

if countParagraphs > 1 then
	-- the offset of each paragraph in the original text
	set theOffset to 0
	repeat with i from 1 to countParagraphs - 1
		set theParagraph to item i of |paragraphs|
		-- Determine single line ending
		set paragraphCount to (count theParagraph)
		set lineEnding to item (paragraphCount + 1 + theOffset) of textItems
		-- check if the ending is double (like \r\n)
		set theNext to item (paragraphCount + 2 + theOffset) of textItems
		if theNext is in endindgs then set lineEnding to lineEnding & theNext
		-- add current retrived text
		set newText to newText & word 2 of theParagraph & lineEnding
		set theOffset to theOffset + paragraphCount + (count lineEnding)
	end repeat
end if

-- add last paragraph (without ending)
set newText to newText & word 2 of item -1 of |paragraphs|

This is the Regular Expression solution, it removes all occurrences of 6 digits followed by one or more whitespace characters

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theLines to "206487    11010
206486    11061
206485    11068
206242    10837
206241    10838
206243    10840
206251    11167
206239    88806
206240    88807
206244    88808
206245    88809
206248    88854
206249    88818
206238    88819
206250    88849
206247    88852"

set cocoaString to current application's NSString's stringWithString:theLines
set theResult to (cocoaString's stringByReplacingOccurrencesOfString:"\\d{6}\\s+" withString:"" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:cocoaString's |length|()}) as text

My suggestion which works with both tab and space separators.

-- I abbreviated theText for brevity's sake
-- paragraph 1 uses space separator; paragraph 2 uses tab separator; paragraph 3 uses both
set theText to "206487 11010
206486	11061
206485	 11068
206242 10837"

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tab, space}

set theNewText to {}
repeat with aParagraph in (paragraphs of theText)
	set the end of theNewText to text item -1 of aParagraph -- last 5 characters
	-- set the end of theNewText to text 2 thru -1 of (text item -1 of aParagraph) -- last 4 characters
end repeat

set AppleScript's text item delimiters to linefeed
set theNewText to theNewText as text
set AppleScript's text item delimiters to ATID

theNewText

The OP has 1 tab there. And linefeed endings :smiley: I had to go into his post through the Quote function of the site and copy what they pasted.

Script Debugger says for the text:

“206487\t11010\n206486\t11061\n206485\t11068\n206242\t10837\n206241\t10838\n206243\t10840\n206251\t11167\n206239\t88806\n206240\t88807\n206244\t88808\n206245\t88809\n206248\t88854\n206249\t88818\n206238\t88819\n206250\t88849\n206247\t88852”

Try the following I pasted. It has 1 space and linefeed endings. You can check using Quote function with this post:

206487 11010
206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852

For those who is interested to test with different tab, space, \r, \n and \r\n combinations:


set endindgs to {linefeed, return}

set theText to "206487 11010" & return & linefeed & "206486 11061
206485     11068
206242 10837
206241 10838
206243 10840
206251         11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852"

set textItems to theText's characters
set |paragraphs| to paragraphs of theText
set countParagraphs to count |paragraphs|

-- build new text
set newText to ""

if countParagraphs > 1 then
	-- the offset of each paragraph in the original text
	set theOffset to 0
	repeat with i from 1 to countParagraphs - 1
		set theParagraph to item i of |paragraphs|
		-- Determine single line ending
		set paragraphCount to (count theParagraph)
		set lineEnding to item (paragraphCount + 1 + theOffset) of textItems
		-- check if the ending is double (like \r\n)
		set theNext to item (paragraphCount + 2 + theOffset) of textItems
		if theNext is in endindgs then set lineEnding to lineEnding & theNext
		-- add current retrived text
		set newText to newText & word 2 of theParagraph & lineEnding
		set theOffset to theOffset + paragraphCount + (count lineEnding)
	end repeat
end if

-- add last paragraph (without ending)
set newText to newText & word 2 of item -1 of |paragraphs|

Thank you for helping out! I added “set the clipboard to newText” to get the result to the clipboard. The result prints out at in the Result window and not in the TextWrangler document. How do I get the result show up in the TextWrangler document?

Thank you!

Hi.

KniazidisR’s solutions set a variable to ‘theText’s text items’ without first specifying a value for the ‘text item delimiters’. When the delimiters are at their default value of {“”}, a ‘text item’ is the same as a ‘character’. Since this is apparently what’s meant here, it would be both safer and more correct (and possibly more efficient) to use the term ‘characters’ instead. It always means characters no matter how the text item delimiters may happen to be set at the time.

  1. The suggestion of Nigel Garvey is good. Replace
set textItems to theText's text items

with

set textItems to theText's characters
  1. I have not TextWrangler installed to play with it. But… I think you can use set text of front document to newText. This way I think your original text should be replaced with new. If you want to keep original text unchanged, then you can wrote something like this:
set newDocument to make new document
set text of newDocument to newText

thank you!