[英]Find and replace in bash for variables
I am using bash and I was wondering if there is a nice way to do a find and replace on a file using bash. 我正在使用bash,我想知道是否存在使用bash查找和替换文件的好方法。 In my case I want to turn a placeholder variable into the orignal variable, working with any number of different variables.
就我而言,我想将一个占位符变量转换为原始变量,并使用任意数量的不同变量。 For example:
例如:
"$PLACEHOLDER_ABC" -> $ABC
"$PLACEHOLDER_123" -> $123
"$PLACEHOLDER_qwe" -> $qwe
I am not sure where to start, should I be using find, sed, a while loop, or all of the above? 我不确定从哪里开始,应该使用find,sed,while循环还是以上所有功能?
Something like 就像是
sed 's/\$PLACEHOLDER_/\$/g' file
would remove PLACEHOLDER_
everywhere it is found after a dollar sign, effectively truncating the parameter expansion where it occurs. 会在出现美元符号后的所有位置删除
PLACEHOLDER_
,从而有效地截断了出现参数扩展的位置。
You may want to have a look at "m4" the macro processor. 您可能需要看一下“ m4”宏处理器。 The manpages are here .
手册在这里 。 It is included in most Linux/Unix distros.
它包含在大多数Linux / Unix发行版中。
Here is a little example: 这是一个小例子:
define(`hello', `Hello, World')
hello, welcome to m4!
If we run this file through m4 with the command 如果我们使用以下命令通过m4运行该文件
m4 sample.m4 > sample.txt
it produces the following output: 它产生以下输出:
Hello, World, welcome to m4!
查找文件名-exec sed -i's / \\ $ PLACEHOLDER _ / \\ $ / g'{} \\;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.