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
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: ‘<’”
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
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
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