fileContents

I’m newbie with GREP
Can anybody help me with replacing in file “abc.ini” a part of string that goes after “/cppr.width” up to EOL with my value?

in javascript it looks like
fileContents.replace(/(.+/cppr.width).+/, “$1”+“new value”)

How to implement such replace in shell?

Hello,

Grep is a tool that prints lines matching a pattern. To perform substitutions use instead commands like sed, awk, perl or ruby.

set p2file to "/path/to/abc.ini"
set rstring to " my replacement string"

-- makes an "in-place" edit of the .ini file & generates a backup of the original file with extension ".bak"
do shell script "perl -p -i.bak -e 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file 
-- makes an "in-place" edit without backup
--do shell script "perl -pi -e 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file

hth

 set res to do shell script "perl -pi -e 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file

cutt off the rest of file after finding the pattern

If I use sed

 "sed -e 's/\\(cppr\\.height \\).*/\\1 " & rstring "/' " & quoted form of p2file

when I check the result in Script Debugger it seems OK
but nothing happens in file. Where is a bug?

Why not a pure AppleScript solution?


set p2file to ((path to desktop as Unicode text) & "abc.ini")
set s_string to "/cppr.width"
set r_string to " my replacement string"

try
	set {TID, text item delimiters} to {text item delimiters, s_string}
	set ff to open for access file p2file with write permission
	set theData to read ff
	set theData to text items of theData
	if (count theData) = 2 then
		set theData to item 1 of theData & s_string & r_string
		set eof of ff to 0
		write theData to ff starting at eof
	end if
	close access ff
on error
	try
		close access file p2file
	end try
end try
set text item delimiters to TID

Hello,
this is probably due to the fact that your file uses carriage-returns instead of line-feeds. So you have to convert them.

… this work for me with a CR delimited file…

set res to do shell script "perl -i.bak -pe 's/\\r/\\n/g; s/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file

…Mike, this is Sed’s normal behavior… it doesn’t make any “in-place” substitution, his result is send to standard output.
You have to redirect the standard output to a new file


set p2input to  POSIX path of ((path to desktop as Unicode text) & "abc.ini")
set p2output to  POSIX path of ((path to desktop as Unicode text) & "abc_new.ini")
set rstring to " my replacement string"

set res to do shell script "tr '\\r' ' \\n' <" & space & quoted form of p2input & " | sed  -E 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' >" & space & quoted form of p2output

Regards
Clement

Actually I need 8 search/replaces to do so I think Applescript isn’t ideal tool to process text strings

But the question is how to implement search/replace inside “tell Illustartor” block?

If replacing goes inside block, error thrown, if outside - all OK
it
But 8 switches to Ill and back isn’t good way, isn’t it?

AppleScript is probably the fastest way to perform multiple search / replace operations :wink:

OK,
but how to replace in Applescript carriage-returns with line-feeds?


set a to paragraphs of (read (choose file))

set {TID, text item delimiters} to {text item delimiters, linefeed}
set a to a as text
set text item delimiters to TID
a

Note: linefeed has been introduced in Leopard, in Tiger use ASCII character 10

It’s fine, but now… how to to pass “var a” to find/replace functions?

tell application "Adobe Illustrator"
	set aD to document 1
	set w to width of aD
	set h to height of aD
end tell

set myFile to (POSIX file "/Own/separationsX1")
set a to paragraphs of (read myFile)
set {TID, text item delimiters} to {text item delimiters, linefeed}
set a to a as text
set text item delimiters to TID

mySizes("cppr.height", h)
mySizes("cppr.width", w)
mySizes("ppr.height", h)
mySizes("ppr.width", w)
mySizes("ppr.image.right", w)
mySizes("ppr.image.bottom", h)

on mySizes(s, r)
	try
		set {TID, text item delimiters} to {text item delimiters, s}
		set ff to open for access myFile with write permission
		set theData to read ff
		set theData to text items of theData
		if (count theData) = 2 then
			set theData to item 1 of theData & s & r
			set eof of ff to 0
			write theData to ff starting at eof
		end if
		close access ff
	on error
		try
			close access myFile
		end try
	end try
	set text item delimiters to TID
end mySizes

sorry, I read EOF instead of EOL.
what’s about this “conventional” solution


tell application "Adobe Illustrator"
	tell document 1 to set {w, h} to {width, height}
end tell

try
	set myFile to (POSIX file "/Own/separationsX1") as alias
	set ff to open for access myFile with write permission
	set theLines to paragraphs of (read ff)
	repeat with aLine in theLines
		if aLine contains "cppr.height" then
			set contents of aLine to stripString(aLine, "cppr.height", h)
		else if aLine contains "cppr.width" then
			set contents of aLine to stripString(aLine, "cppr.width", w)
		else if aLine contains "ppr.height" then
			set contents of aLine to stripString(aLine, "ppr.height", h)
		else if aLine contains "ppr.width" then
			set contents of aLine to stripString(aLine, "ppr.width", w)
		else if aLine contains "ppr.image.right" then
			set contents of aLine to stripString(aLine, "ppr.image.right", w)
		else if aLine contains "ppr.image.bottom" then
			set contents of aLine to stripString(aLine, "ppr.image.bottom", h)
		end if
	end repeat
	set {TID, text item delimiters} to {text item delimiters, linefeed}
	set theLines to theLines as text
	set text item delimiters to TID
	set eof of ff to 0
	write theLines to ff starting at eof
	close access ff
on error
	try
		close access myFile
	end try
end try

on stripString(listElement, s, r)
	set o to ((offset of s in listElement) - 1)
	if o = 0 then
		return r
	else
		return text 1 thru o of listElement & r
	end if
end stripString

Look at result:
/ppr.custom 1
/ppr.default 0
→ /663.307067871094
→ /850.393676757812
/ppr.image.left 0.0
/ppr.image.top 0.0

the string beginings “cppr…” erased, but it should remains :frowning:


tell application "Adobe Illustrator"
	tell document 1 to set {w, h} to {width, height}
end tell

try
	set myFile to (POSIX file "/Own/separationsX1") as alias
	set ff to open for access myFile with write permission
	set theLines to paragraphs of (read ff)
	repeat with aLine in theLines
		if aLine contains "cppr.height" then
			set contents of aLine to stripString(aLine, "cppr.height ", h)
		else if aLine contains "cppr.width" then
			set contents of aLine to stripString(aLine, "cppr.width ", w)
		else if aLine contains "ppr.height" then
			set contents of aLine to stripString(aLine, "ppr.height ", h)
		else if aLine contains "ppr.width" then
			set contents of aLine to stripString(aLine, "ppr.width ", w)
		else if aLine contains "ppr.image.right" then
			set contents of aLine to stripString(aLine, "ppr.image.right ", h)
		else if aLine contains "ppr.image.bottom" then
			set contents of aLine to stripString(aLine, "ppr.image.bottom ", w)
		end if
	end repeat
	set {TID, text item delimiters} to {text item delimiters, linefeed}
	set theLines to theLines as text
	set text item delimiters to TID
	set eof of ff to 0
	write theLines to ff starting at eof
	close access ff
on error
	try
		close access myFile
	end try
end try

on stripString(listElement, s, r)
	set o to ((offset of s in listElement) - 1)
	if o = 0 then
		return s & r
	else
		return text 1 thru o of listElement & s & r
	end if
end stripString

THANX A LOT!!!

It’s non-usual way to search/replace for those who familiar with javascript