私はこのスレッドが役に立ちました - 私は自分自身の問題に答えを加えると思っていました。
javascriptのノードアプリケーションからデータベース構成ファイル(datastax cassandra)を編集し、ファイル内の設定のうち、文字列を照合してそれに続く行を置き換える必要があるとします。
これは私の解決策でした。
dse_cassandra_yaml='/etc/dse/cassandra/cassandra.yaml'
// a) find the searchString and grab all text on the following line to it
// b) replace all next line text with a newString supplied to function
// note - leaves searchString text untouched
function replaceStringNextLine(file, searchString, newString) {
fs.readFile(file, 'utf-8', function(err, data){
if (err) throw err;
// need to use double escape '\\' when putting regex in strings !
var re = "\\s+(\\-\\s(.*)?)(?:\\s|$)";
var myRegExp = new RegExp(searchString + re, "g");
var match = myRegExp.exec(data);
var replaceThis = match[1];
var writeString = data.replace(replaceThis, newString);
fs.writeFile(file, writeString, 'utf-8', function (err) {
if (err) throw err;
console.log(file + ' updated');
});
});
}
searchString = "data_file_directories:"
newString = "- /mnt/cassandra/data"
replaceStringNextLine(dse_cassandra_yaml, searchString, newString);
実行した後、それは新しいものに設定し、既存のデータディレクトリを変更します:
設定ファイルの前に:
data_file_directories:
- /var/lib/cassandra/data
設定ファイルの後:
data_file_directories:
- /mnt/cassandra/data
出典
2016-03-03 13:22:48
JRD
'value'は可変ですか? – Dogbert