Chan Chen Coding...

Use Sed in Multi-Files Text Processing

During the software development, developers always need to modify multi code files since some bugs or requirement change. All of this modification may be similar, such as and insert the following code into JavaScript(<script> /*JavaScript Section*/</scripte>) section in JSP page, let us say we have A.jsp, B.jsp, and C.jsp. 
function insertMe(){
    alert(“Hello”);
}
To complete this task, normally, we can open all three jsp file one by one, and copy + paste the function insertMe() into code. But if we have more files, it is boring and annoy to open echo file and insert the code.
There is other smarter way to achieve this task, sed utility in Linux. In Linux, the sed provides the developer an convinces way. Just one command:
sed –i ‘/<script>/ a\function insertMe(){\talert(“Hello”);\n}\n ‘ [ABC].jsp
This script means: append function insertMe(){…} after <script> in A.jsp, B.jsp and C.jsp.
Sed also provide other powerful feature, for more, please dive into ‘man sed’. Below is an example of replace.
sed –e ‘s/Hello/Hello World‘ [ABC].jsp
This script means replace all Hello with Hello World in A.jsp, B.jsp, and C.jsp.
Because sed is a line oriented program.



-----------------------------------------------------
Silence, the way to avoid many problems;
Smile, the way to solve many problems;

posted on 2012-02-18 15:49 Chan Chen 阅读(227) 评论(0)  编辑  收藏 所属分类: Linux


只有注册用户登录后才能发表评论。


网站导航: