使用基于sed/awk的linux替换字符串 [英] Replacing string in linux using sed/awk based
本文介绍了使用基于sed/awk的linux替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要替换这个
#!/usr/bin/env bash
与此
#!/bin/bash
我尝试了两种方法
方法1
original_str="#!/usr/bin/env bash"
replace_str="#!/bin/bash"
sed s~${original_str}~${replace_str}~ filename
方法2
line=`grep -n "/usr/bin" filename`
awk NR==${line} {sub("#!/usr/bin/env bash"," #!/bin/bash")}
但是他们两个都不起作用.
But both of them are not working.
推荐答案
您不能在BASH中的双引号内使用!
.否则将进行历史扩展.
You cannot use !
inside a double quotes in BASH otherwise history expansion will take place.
您可以这样做:
original_str='/usr/bin/env bash'
replace_str='/bin/bash'
sed "s~$original_str~$replace_str~" file
#!/bin/bash
这篇关于使用基于sed/awk的linux替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文