Text file as source of replace for find and replace

I have found ways to do a find and replace on a file. But what if I want to have the replace values populated by a text file as well? Can Applescript do this or do I need to go after perl/sed/awk?

Anyone have any code examples?

Assuming a new value on every line:

set xxx to every paragraph of (read "/Users/Andy/Desktop/replace_values.txt")

Nice! I will give it a try. :smiley:

So I took the example above and tried to meld it together with the example from this page: http://macscripter.net/viewtopic.php?id=11374

Big surprise it is not working. Do I need to have the Sed portion loop? Have any ideas?

Here is the error: " sed: 1: “s/applestart”: unterminated substitute pattern"

  set inputFile to quoted form of "/Users/andy/Desktop/filename.txt" --> input file
set outputFile to quoted form of "/Users/andy/Desktop/filename3.txt" --> output file
set searchFor to every paragraph of (read "/Users/andy/Desktop/search.txt")
set replaceWith to every paragraph of (read "/Users/andy/Desktop/replace.txt")

bigShellSearchReplaceTextInFile(inputFile, outputFile, searchFor, replaceWith)

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

Thanks,
Andy

Model: iMac 27 mid 2010
AppleScript: 2.2.1
Browser: Safari
Operating System: Mac OS X (10.7)

Hi,

you could work with AS . Assuming your textfile isn’t to large … It is? Then you indeed should search for faster solutions

set sL to paragraphs of (read (choose file with prompt "Choose SearchList"))
set rL to paragraphs of (read (choose file with prompt "Choose ReplaceList"))


set sourceFile to choose file with prompt "Choose File to operate on"
tell application "System Events" to set aName to name of disk item (sourceFile as text)

set oldText to read sourceFile
set newTxt to textReplacer(oldText, sL, rL)
set newFile to (path to desktop as text) & "finished_" & aName
write_to_file(newTxt, newFile, false)




on textReplacer(input, srchList, replList)
	repeat with i from 1 to (count of srchList)
		set srch to item i of srchList
		set repl to item i of replList
		set AppleScript's text item delimiters to srch
		set the temp to every text item of input
		set AppleScript's text item delimiters to repl
		set input to temp as text
	end repeat
	set AppleScript's text item delimiters to ""
	return input
end textReplacer


on write_to_file(this_data, target_file, append_data)
	try
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Andy, you need to prepare the sed statement for each one of the terms with something like:

set inputFile to quoted form of "/Users/andy/Desktop/filename.txt" --> input file
set outputFile to quoted form of "/Users/andy/Desktop/filename3.txt" --> output file
set searchFor to every paragraph of (read "/Users/andy/Desktop/search.txt")
set replaceWith to every paragraph of (read "/Users/andy/Desktop/replace.txt")
set termCount to count of searchFor
set sedCommand to {"sed"}

repeat with i from 1 to termCount
	if i is not termCount then
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g" & space
	else
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g " & inputFile & " > " & outputFile
	end if
end repeat

set sedCommand to sedCommand as text
do shell script sedCommand

Both solutions worked extremely well! Thank you for your input.

So I tried to integrate both approaches without much success.

 set inputFile to (read (choose file with prompt "Choose File to Operate On"))

set searchFor to every paragraph of (read (choose file with prompt "Choose SearchList"))
set replaceWith to every paragraph of (read (choose file with prompt "Choose ReplaceList"))

set outputFile to (path to desktop as text) & "finished"

set termCount to count of searchFor
set sedCommand to {"sed"}

repeat with i from 1 to termCount
	if i is not termCount then
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g" & space
	else
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g " & inputFile & " > " & outputFile
	end if
end repeat

set sedCommand to sedCommand as text

do shell script sedCommand with administrator privileges

These are the errors that I am getting (any ideas?):

error “sed: Apple: No such file or directory
sed: tango: No such file or directory
sed: start: No such file or directory
sed: age: No such file or directory
sed: end: No such file or directory
sed: apple: No such file or directory
sed: top: No such file or directory
sed: end: No such file or directory
sed: start: No such file or directory
sed: apple: No such file or directory
sed: tango: No such file or directory
sed: mango: No such file or directory
sed: HD:Users:andy:Desktop:finished: No such file or directory” number 1

Hi,

in- and output-file have to be posix-paths … have a close look at adayzones’ script

Maybe the simplest coding solution, provided by satimage osax, would be a good start …

--google for satimage osax to run this script ...


set sL to paragraphs of (read (choose file with prompt "Choose SearchList"))
set rL to paragraphs of (read (choose file with prompt "Choose ReplaceList"))


set sourceFile to choose file with prompt "Choose File to operate on"
tell application "System Events" to set aName to name of disk item (sourceFile as text)

set oldText to read sourceFile
set newFile to (path to desktop as text) & "finished_" & aName

writetext (change sL into rL in oldText) to file newFile encoding "MACINTOSH"
(*
Comment: ENCODING -> ("MACINTOSH", "UTF-8", "UTF-16", "ISO-8859-1", "windows-1252".). Default: "UTF-8"
*)





You were pretty close. Try this:

set inputFile to POSIX path of (choose file with prompt "Choose File to Operate On")
set searchFor to every paragraph of (read (choose file with prompt "Choose SearchList"))
set replaceWith to every paragraph of (read (choose file with prompt "Choose ReplaceList"))
set outputFile to POSIX path of (path to desktop as text) & "finished"
set termCount to count of searchFor
set sedCommand to {"sed"}

repeat with i from 1 to termCount
	if i is not termCount then
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g" & space
	else
		set end of sedCommand to space & "-e 's/" & item i of searchFor & "/" & text of item i of replaceWith & "/'g " & inputFile & " > " & outputFile
	end if
end repeat

set sedCommand to sedCommand as text
do shell script sedCommand with administrator privileges

That’s funny. I didn’t put in the POSIX and that’s why it didn’t work.

Why did we need to put in the POSIX Path for the input and output file but not for the other two?

I guess I don’t fully understand the relation between how the posix path/unix paths are passed vs how applescript uses the “:”.

Thanks for the help!

I used a posix path for the input and output file because they were part of the “do shell script” command.

That’s awesome. Thanks for letting me know. It makes sense since sed is a Unix command that it would require a POSIX path all of the ones that are direct.