| 
					 1 2  | 
						This is an {{VAR1}} {{VAR2}}. It should work across multiple lines in this {{VAR2}}.  | 
					
设置以下环境变量:
| 
					 1 2  | 
						VAR1='example' VAR2='file'  | 
					
时应该输出:
| 
					 1 2  | 
						This is an example file. It should work across multiple lines in this file.  | 
					
可以使用:
| 
					 1  | 
						perl -pe 's/{{(.*?)}}/$ENV{$1}/g' filename  | 
					
其工作原理如下:
- s/pattern/replacement/g是替换命令,pattern为Perl的正则表达式,g标志使得所有匹配都被替换(不加的话则只替换第一个);
 - 在pattern中,.*?为非贪婪地匹配,,以便在包含foo {{VAR1}} bar {{VAR2}} baz的行中模式{{.*?}}仅匹配{{VAR1}}而不是{{VAR1}} bar {{VAR2}};
 - $1表示捕获{{和}}之间的第一个分组;
 - $ENV{$1}是名为$1的环境变量的值;
 
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Linux下命令行替换文件中出现的所有占位符
进城务工人员小梅