<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nutty Coder &#187; sed</title>
	<atom:link href="http://blog.nuttycoder.com/tag/sed/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nuttycoder.com</link>
	<description>tech articles</description>
	<lastBuildDate>Wed, 08 Dec 2010 13:26:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>用Shell Script导出SVN版本间变更的文件</title>
		<link>http://blog.nuttycoder.com/2010/06/04/export-updated-files-in-svn-with-shell-scrip/</link>
		<comments>http://blog.nuttycoder.com/2010/06/04/export-updated-files-in-svn-with-shell-scrip/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 09:52:12 +0000</pubDate>
		<dc:creator>Wang Xiaoxing</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[SVN]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://blog.nuttycoder.com/?p=150</guid>
		<description><![CDATA[#!/bin/bash # 配置 # TODO 将配置信息保存在外部文件中以方便产生不同目标的导出。 START=9981 END=10054 VER=eom BUILD=v1 OUTPUT_PATH=builds/$VER/$BUILD/files/ # 清理 if [ -d &#34;$OUTPUT_PATH&#34; ]; then rm -rf $OUTPUT_PATH fi # 得到更改过的文件数量和文件名列表 # svn diff --summarize 返回&#34;Status \t Filename&#34; COUNT=`svn diff --summarize -r $START:$END &#124; wc -l` Diff=`svn diff --summarize -r $START:$END` until [ $COUNT -lt &#34;1&#34; ] do # echo &#34;$Diff&#34; [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: plain; title: ;">
#!/bin/bash

# 配置
# TODO 将配置信息保存在外部文件中以方便产生不同目标的导出。
START=9981
END=10054
VER=eom
BUILD=v1
OUTPUT_PATH=builds/$VER/$BUILD/files/

# 清理
if [ -d &quot;$OUTPUT_PATH&quot; ]; then
	rm -rf $OUTPUT_PATH
fi

# 得到更改过的文件数量和文件名列表
# svn diff --summarize 返回&quot;Status \t Filename&quot;

COUNT=`svn diff --summarize -r $START:$END | wc -l`
Diff=`svn diff --summarize -r $START:$END`
until [ $COUNT -lt &quot;1&quot; ]
do
	# echo &quot;$Diff&quot; 的用法也许不合适。
	# awk pattern action, 'print $2' 即输出第二列的内容
	# grep -v 反向选择，即不含模式的。此为“选取路径不以builds开头的文件”

	File=&quot;`echo &quot;$Diff&quot; | awk {'print $2'} | grep &quot;^builds&quot; -v | head -$COUNT | tail -1`&quot;
	Dir=`dirname $File`
	Name=`basename $File`

	# 检查目标路径，如不存在则递归的创建

	if [ ! -d &quot;$OUTPUT_PATH$Dir&quot; ]; then
		mkdir $OUTPUT_PATH$Dir -p
	fi

	# 将文件中的string1替换为string2，并且输出到目标路径
	# 此处是将两个操作合并，也可先做cp，再替换
	sed 's/string1/string2/g' $File &amp;gt; $OUTPUT_PATH$Dir/$Name

	# 如果目标文件中含有string3，则将之替换为string4，在目标文件上操作。
	if grep &quot;string3&quot; $File &amp;gt; /dev/null
	then
		sed -in-place -e 's/string3/string4/g' $OUTOUT_PATH$Dir/$Name
	fi

	# 去除string5
	if grep &quot;string5&quot; $File &amp;gt; /dev/null
	then
		sed -in-place -e 's/string5\///g' $OUTPUT_PATH$Dir/$Name
	fi

	# more replace here
	# TODO

	# replace configure variables, need define configure file
	# TODO

	COUNT=`expr $COUNT - 1`
done

# export changed database table
# TODO

# commit to svn
# TODO
</pre>
<p>另外记录一条批量替换字串的命令：</p>
<pre class="brush: plain; title: ;">
sed -i 's/pattern/new/g' `grep -rl 'pattern' *`
# sed -i: in place，在原文件上操作
# grep -r: recursive，递归的，遍历所有子目录下的文件
# grep -l: file with matches，返回包含有模式的文件
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.nuttycoder.com/2010/06/04/export-updated-files-in-svn-with-shell-scrip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

