2010年11月26日星期五

用sed批量替换文件中的字符

由于缺乏模板系统,以前使用html编写的笔记已经很久没有更新了,如果需要对页面内容进行一些更新(比如要更换一下页面中的CSS文件路径),可以用sed完成。对于多个文件的批量处理,整理步骤如下:



1 先创建一个temp目录,将需要更新的文件放到这个临时目录工作下:
cp *.html temp/

2 用ll + awk组合生成脚本文件:
ls -1 * | awk '{print "sed s#/style.css#/blog/styles_zh-cn.css#g "$1" > ../"$1}' > sed.sh
生成后的脚本文件如下:
sed s#/style.css#/blog/styles_zh-cn.css#g acdsee.html > ../acdsee.html
sed s#/style.css#/blog/styles_zh-cn.css#g ant.html > ../ant.html
sed s#/style.css#/blog/styles_zh-cn.css#g apache_install.html > ../apache_install.html
sed s#/style.css#/blog/styles_zh-cn.css#g awstats.html > ../awstats.html
sed s#/style.css#/blog/styles_zh-cn.css#g cache.html > ../cache.html
sed s#/style.css#/blog/styles_zh-cn.css#g click.html > ../click.html
sed s#/style.css#/blog/styles_zh-cn.css#g cms.html > ../cms.html
sed s#/style.css#/blog/styles_zh-cn.css#g comment.html > ../comment.html
sed s#/style.css#/blog/styles_zh-cn.css#g compress.html > ../compress.html
sed s#/style.css#/blog/styles_zh-cn.css#g cvs_card.html > ../cvs_card.html
sed s#/style.css#/blog/styles_zh-cn.css#g default.html > ../default.html
sed s#/style.css#/blog/styles_zh-cn.css#g dev.html > ../dev.html
sed s#/style.css#/blog/styles_zh-cn.css#g gnu.html > ../gnu.html
sed s#/style.css#/blog/styles_zh-cn.css#g google_ads.html > ../google_ads.html
sed s#/style.css#/blog/styles_zh-cn.css#g google.html > ../google.html
sed s#/style.css#/blog/styles_zh-cn.css#g google_url.html > ../google_url.html
sed s#/style.css#/blog/styles_zh-cn.css#g hello_unicode.html > ../hello_unicode.html
sed s#/style.css#/blog/styles_zh-cn.css#g indent_tools.html > ../indent_tools.html
sed s#/style.css#/blog/styles_zh-cn.css#g index.html > ../index.html
sed s#/style.css#/blog/styles_zh-cn.css#g link_pop_check.html > ../link_pop_check.html
sed s#/style.css#/blog/styles_zh-cn.css#g lucene.html > ../lucene.html
sed s#/style.css#/blog/styles_zh-cn.css#g mysql.html > ../mysql.html
sed s#/style.css#/blog/styles_zh-cn.css#g nat.html > ../nat.html
sed s#/style.css#/blog/styles_zh-cn.css#g oe2html.html > ../oe2html.html
sed s#/style.css#/blog/styles_zh-cn.css#g oracle.html > ../oracle.html
sed s#/style.css#/blog/styles_zh-cn.css#g resin.html > ../resin.html
sed s#/style.css#/blog/styles_zh-cn.css#g rotate_merge_log.html > ../rotate_merge_log.html
sed s#/style.css#/blog/styles_zh-cn.css#g sed.sh > ../sed.sh
sed s#/style.css#/blog/styles_zh-cn.css#g study.html > ../study.html
sed s#/style.css#/blog/styles_zh-cn.css#g unicode_java.html > ../unicode_java.html
sed s#/style.css#/blog/styles_zh-cn.css#g weblog.html > ../weblog.html
sed s#/style.css#/blog/styles_zh-cn.css#g weblucene.html > ../weblucene.html
sed s#/style.css#/blog/styles_zh-cn.css#g xml_potato.html > ../xml_potato.html
sed s#/style.css#/blog/styles_zh-cn.css#g xslt.html > ../xslt.html


3 然后执行脚本文件:
shell sed.sh




很多时候批量数据的处理,都可以先做好单个命令,然后通过awk(甚至Excel)登生成批处理脚本。然后用生成的脚本(shell 或SQL)批量执行。

JieEr提供了一个更简便的方法:
sed -in-place -e 's/abc/cba/g' *

-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)

没有评论:

发表评论