使用给定文件中的适当值更新特定条目

时间:2013-02-05 16:33:19

标签: bash shell

如何使用给定文件中的适当值更新特定条目

假设我有一个文件名hello.conf,

/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args=""
/agent/process="stophelloJava",stop="/opt/stophello.sh",stop_dir=/opt/hello",stop_args="",stop_timeout="30"

使用

更新starthelloJava
start_args="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/"

输出

/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/"

清空进程start_args

starthelloJava
/agent/process="starthelloJava",start="/opt/starthello.sh",start_dir="/opt/hello",start_args=""
/agent/process="stophelloJava",stop="/opt/stophello.sh",stop_dir=/opt/hello",stop_args="",stop_timeout="30"

我正在做的方式很长,我根据输入重写整个文件。

这是我的apprach效率不高

cp hello.conf hello.conf_bak
remove entry first
cat /opt/CSCObac/agent/conf/agent.conf_bak | grep -v /opt/starthello.sh  > hello.conf
echo "/agent/process=\"starthelloJava\",start=\"/opt/starthello.sh\",start_dir=\"/opt/hello\",start_args=\"-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/\" >> hello.conf

1 个答案:

答案 0 :(得分:1)

set -- '-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/'
sed -i -r "s!(.*starthelloJava.*=).*!\1\"$1\"!" hello.conf

或清空值

set -- ''
sed -i -r "s!(.*starthelloJava.*=).*!\1\"$1\"!" hello.conf