使用Xcode Run Script bin/sh中的sed查找并替换多行
我有一个Cocos2D tmx文件,它非常类似于xml,并包含回车符和空格.
I have a Cocos2D tmx file which is very much like an xml and includes carriage returns and spaces.
我的要求是:
在Resources/maps_sideScrolling/
In every tmx file in Resources/maps_sideScrolling/
查找以及
<tileset firstgid="1"
和第一个出现的
<layer name="background"
并替换为Resources/maps_sideScrolling/tileProperties.txt的内容
and replace with the contents of Resources/maps_sideScrolling/tileProperties.txt
我尝试了以下操作,但没有结果.问题是由要搜索的字符串有多行引起的.
I've tried the following with no result. The problem is caused by the string to be searched has multiple lines.
sed -i '' 's{<tileset firstgid="1.*<layer name="background"{Resources/maps_sideScrolling/tileProperties.txt{g' Resources/maps_sideScrolling/*.tmx;
这是我要编辑的tmx片段的粘贴框: http://pastebin.com/wr39zj1r
Here's a pastebin of the tmx snippet that I want to edit: http://pastebin.com/wr39zj1r
Geek使用python对TMX映射文件执行此类操作.只是要考虑的一个选择.
Geek uses python to do this kind of thing to TMX map files. Just an option to consider.
类似这样的事情(但是迭代目录中的所有文件等),并将其保存为.sh文件:
Something like this (but iterating all files in directory etc), and save it as a .sh file:
#!/usr/bin/env python
import re
#you'd open a file and read in the tile properties thing
fakeTileProperties = "<tileproperties>1</tileproperties>\r"
f = open( "file1.tmx", "rU")
fo = open( "outputfile.tmx", "wc");
#read source file
s = f.read();
#find what you need
m = re.search("([\W\w]*)(<tileset firstgid=\"1\"[\W\w]*)(<layer name=\"background\"[\W\w]*)", s )
#write out to source file
fo.write(m.group(1))
fo.write(fakeTileProperties)
fo.write(m.group(3));
f.close();
fo.close();
print "done!"
代码会在平铺集firstgid ="1"之前处理内容,以防万一.
The code handles content before the tile set firstgid="1" just in case there is some.
要在Xcode 4中使用这样的脚本,请执行以下操作:
To use a script like this in Xcode 4 do the following:
- 将脚本放入项目文件旁边的文件中,将其命名为
myscript.py
- 使用
chmod +x myscript.py
使脚本文件可执行. - 在Xcode项目中,选择项目和目标,然后选择构建阶段"选项卡,然后创建一个新的运行脚本"构建阶段.
- 保留/bin/sh的默认外壳程序
- 在脚本字段中输入以下内容:
$(SOURCE_ROOT)/myscript.py
- put your script in a file next to your project file, name it
myscript.py
- use
chmod +x myscript.py
to make the script file executable. - in your Xcode project select the project and target and "Build Phases" tab and then create a new "Run Script" build phase.
- leave the default shell of /bin/sh
- put the following into the script field:
$(SOURCE_ROOT)/myscript.py
然后,当您进行构建时,应该看到python脚本已执行.您可以做一个非常简单的测试python文件来测试它(我刚刚做了):
Then when you do a build you should see the python script get executed. You can do a really simple test python file to test this (I just did):
#!/usr/bin/env python
print 'hello from python!'
请注意,运行脚本设置在构建日志中显示环境变量"中的设置对于获取诸如SOURCE_ROOT
之类的环境变量以及定位文件非常有用.
note that the setting in the Run Script setup "Show Environmental variables in build log" is very helpful for getting the environmental variables like SOURCE_ROOT
and such to locate your files.
祝你好运!