search/replace text file

Powerful shell tool which allows search/replace strings in text files. Apparently, no limit in the size of such file, and great speed!!! (eg: 5MB file, 90000 search/replaces = 1,6 seconds, iMac G4 700MHz, not millitimed)
You must provide posix paths to the handler and, preferrable, in quoted form, since this will get ride of some problems with spaces, special characters and so on.

OS version: OS X

set inputFile to quoted form of "/test.txt" --> input file
set outputFile to quoted form of "/test2.txt" --> output file
set searchFor to quoted form of "text" --> search for this
set replaceWith to quoted form of "huh?" --> replace with this

bigShellSearchReplaceTextInFile(inputFile, outputFile, searchFor, replaceWith)

to bigShellSearchReplaceTextInFile(inputFile, outputFile, searchFor, replaceWith)
	do shell script "sed s/" & searchFor & "/" & replaceWith & "/g " & inputFile & " > " & outputFile
end bigShellSearchReplaceTextInFile

Hi :smiley:
Is it a problem to use, well, special characters in the text, “<” in this case. I have tried the following:


set searchFor to quoted form of "<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"20\"/>" --> search for this
	set replaceWith to quoted form of "<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"-1\"/>" --> replace with this
	
	

	do shell script "sed s/" & searchFor & "/" & replaceWith & "/g " & inputFile & " > " & outputFile

I get this:
“sed: 1: "s/<Entry Name="Transpor …": bad flag in substitute command: ‘<’”
:confused:
How could I deal with those characters?

Help appreciated!

With the shell, yes.

Try quoting the sed function:

do shell script "/usr/bin/sed " & quoted form of ("s/" & searchFor & "/" & replaceWith & "/g") & " " & inputFile & " > " & outputFile

Now I get this:
sed: 1: "s/‘<Entry Name=“Transpo …”: bad flag in substitute command: ‘’’

I´m not into shell. I have found thiis way here after I have (unsuccessfully) tried this:


on searchReplaceText(SearchText, replaceText, theText)
	tell application "TextEdit"
		set SearchText to SearchText as list
		set replaceText to replaceText as list
		set theText to theText as text
		
		set oldTID to AppleScript's text item delimiters
		repeat with i from 1 to count SearchText
			set AppleScript's text item delimiters to SearchText's item i
			set theText to theText's text items
			set AppleScript's text item delimiters to replaceText's item i
			set theText to theText as text
		end repeat
		set AppleScript's text item delimiters to oldTID
		
		return theText
		
	end tell
end searchReplaceText

…and, as far as I can see, a ‘’'" character isn´t even containt in the text!?:confused:

trial&error solution:


do shell script quoted form of ("sed s/" & searchFor & "/" & replaceWith & "/g") & " " & inputFile & " > " & outputFile

that worked!

Thanks for helping:)

The slash (“/”) is going to cause problems for sed. Also, you shouldn’t be quoting the search and replace variables.

Edit: Give this a shot:

set searchFor to "<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"20\"/>" -- search for this
set replaceWith to "<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"-1\"/>" -- replace with this
set test to "Hello, World!
<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"20\"/>
This is only a test."

findReplace(searchFor, replaceWith, test)


on findReplace(findText, replaceText, sourceText)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to findText
	set sourceText to text items of sourceText
	set AppleScript's text item delimiters to replaceText
	set sourceText to "" & sourceText
	set AppleScript's text item delimiters to ASTID
	return sourceText
end findReplace

same result than in my first version.


on searchReplaceText(SearchText, replaceText, theText)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to SearchText
	set theText to text items of theText
	set AppleScript's text item delimiters to replaceText
	set theText to "" & theText
	set AppleScript's text item delimiters to ASTID
	return theText
end searchReplaceText

It runs fine, but the text is not replaced.

Um. are you doing anything with the result?

set test to findReplace(searchFor, replaceWith, test)

not yet. I just log it to see if it worked out before I write it back in the .xml file where it comes from.

That worked!:smiley:


set theText to my searchReplaceText(SearchText, replaceText, theText)

Thak you. Great help.