Find and replace symbols in text

I am writing a small program and I need to replace symbols such as “]” and “/” in a text file. How would I write a script that will take a text file, then replace these symbols for letters or numbers? I am hoping to do this script in automator, if that makes any difference.

Browser: Safari 525.13
Operating System: Mac OS X (10.4)

I did a search for “search replace” and came up with
many results that should answer your question.

Here are just a couple.

Regards,

Craig

http://bbs.macscripter.net/viewtopic.php?id=16722
http://bbs.macscripter.net/viewtopic.php?id=13468

It’s not glamorous, but this works here on 10.4.11…

Peter B.



set text_example to "bit[of/text"

--set text_example to read (choose file)

set ASTID_Priors to AppleScript's text item delimiters
set AppleScript's text item delimiters to "["
set interim_result to every text item of text_example as list
set AppleScript's text item delimiters to " "
set interim_result to every text item of interim_result as text
set AppleScript's text item delimiters to "/"
set interim_result to every text item of interim_result as list
set AppleScript's text item delimiters to " "
set example_result to every text item of interim_result as text
set AppleScript's text item delimiters to ASTID_Priors

example_result --> "bit of text"

i was trying to loop this to add more characters

set the_list to {"/", "["}
set item_num1 to count of (items of the_list)
set text_example to "bit[of/text"

--set text_example to read (choose file)

set ASTID_Priors to AppleScript's text item delimiters
repeat with i from 1 to item_num1
	set AppleScript's text item delimiters to item i of the_list
	set interim_result to every text item of text_example as list
	set AppleScript's text item delimiters to " "
	set interim_result to every text item of interim_result as text
end repeat

set example_result to interim_result
set AppleScript's text item delimiters to ASTID_Priors

example_result --> "bit of text"

gives “bit of/text” but I can’t figure out why.

here’s the correct text

set the_list to {"[", "/", "_", "]"}
set item_num1 to count of (items of the_list)

set text_example to "bit[of/text_for]now"

--set text_example to read (choose file)
set ASTID_Priors to AppleScript's text item delimiters

repeat with i from 1 to item_num1
	
	set AppleScript's text item delimiters to item i of the_list
	set interim_result to every text item of text_example as list
	set AppleScript's text item delimiters to " "
	set interim_result to every text item of interim_result as text
	set text_example to interim_result
end repeat

set example_result to interim_result
set AppleScript's text item delimiters to ASTID_Priors

example_result --> "bit of text"

You might want to look at ‘tr’ shell command.

A Ruby version would look like this.


puts "bit[of/text_for]now".tr '[/_]', ' '

# bit of text for now

Hi there, I have found this to be the most reliable especially for large files. It’s also the quickest in terms of processing, at least it is in my opinion but I’m happy for someone to show me a better way. :slight_smile:


on run
	set theString to "" as string -- The string you want to alter
	set badChars to {"]", "/"} -- and whatever else you want in here
	
	set newString to ""
	set iChar to 1
	repeat until iChar > (count of theString)
		if badChars contains (item iChar of theString) then
			set newString to newString & "" as string -- what you'd like to replace it with
		else
			set newString to newString & (item iChar of theString) as string
		end if
		set iChar to iChar + 1
	end repeat
	log newString
end run


Alternatively: str_replace

str_replace({"[", "/", "]", "_"}, " ", "bit[of/text_for]now")
--> "bit of text for now"