Chinaunix

标题: 读取参数文件替换另外目标文件中的参数 [打印本页]

作者: ambious888    时间: 2011-09-05 22:27
标题: 读取参数文件替换另外目标文件中的参数
现在有两个文件
一个参数文件
一个操作文件

parameter_file

student_name='Susan';
student_id=10100;
school_name="Beijing No4. Middle School";
teacher_id=110203;

----------------------------------------------------------

student_info

The student student_name is learning at school_name, and the student number is sutdent_id. And the teacher name is teacher_name.

现在要在student_info这个文件中完成parameter_file中涉及到的变量,变量中有可能在后面可能没有用到。如果参数文件没有对应的变量(teacher_name)就用Null 代替并报错,报错信息包括具体的行的位置。


单个替换用sed 应该就可以, 用参数文件这个一时写不出了。谢谢
作者: jason680    时间: 2011-09-06 05:18
现在有两个文件
一个参数文件
一个操作文件

parameter_file

student_name='Susan';
student_id=10 ...
ambious888 发表于 2011-09-05 22:27


$ awk 'FNR==NR{split($0,k,"[=\x27\";]+");a[k[1]]=k[2];next }{for(n in a)sub(n,a[n]);print}' parameter_file student_info
The student Susan is learning at Beijing No4. Middle School, and the student number is sutdent_id. And the teacher name is teacher_name.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2