一个shell脚本,用于通过参数更改文件中的值 [英] A shell script to to change a value in a file with a parameter

查看:639
本文介绍了一个shell脚本,用于通过参数更改文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位etl开发人员,对shell脚本非常陌生,

I am an etl developer, am very new to shell scripting,

我的文件是param.prm

My file is param.prm

[3_go:wf_test:s_test]

$Dbconnection=abcd

$Dbstring=qwert

我需要一个shell脚本来读取上述文件,并在每次运行脚本时使用传递的变量动态更改$ dbconnection(现在为abcd)的值.我该怎么办?

I need a shell script to to read the above file and change the value of $dbconnection (now abcd) dynamically with the variable I pass , every time I run the script. How can I do that?

推荐答案

$ sed 's/\($Dbconnection=\).*/\1NewValue/' param.prm 
[3_go:wf_test:s_test]

$Dbconnection=NewValue

$Dbstring=qwert

要就地更改文件,请使用-i选项.对于GNU sed:

To change the file in-place, use the -i option. For GNU sed:

sed -i 's/\($Dbconnection=\).*/\1NewValue/' param.prm 

对于BSD(OSX)sed:

For BSD (OSX) sed:

sed -i "" 's/\($Dbconnection=\).*/\1NewValue/' param.prm 

这篇关于一个shell脚本,用于通过参数更改文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆