2013-02-05 24 views
2

如何更新適當的值在給定文件中的特定條目,更新適當的值的特定條目在給定文件

讓說,我有一個文件名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_argsstarthelloJava

/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 
+0

我沒有看到任何替代,除非你能保證長度將永遠同意(墨菲_將確保他們不是在你已經忘記的時候)。 Perl有「inplace munging of files」,檢查'perlrun(1)',我相信GNU'sed(1)'也會(它們會在末尾寫入臨時文件並重命名)。 – vonbrand

+0

@ patter只有固定/ agent/process =「」,start =「」,start_dir =「」該值僅固定爲start和start_dir – anish

回答

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 
相關問題