मुझे यह धागा उपयोगी लगा - इसलिए मैंने सोचा कि मैं अपनी समस्या का उत्तर दूंगा।
मैं जावास्क्रिप्ट में एक नोड एप्लिकेशन से डेटाबेस कॉन्फ़िगरेशन फ़ाइल (डेटास्टैक्स कैसेंड्रा) को संपादित करना चाहता था और फ़ाइल में एक सेटिंग्स के लिए मुझे एक स्ट्रिंग पर मिलान करने की आवश्यकता थी और फिर इसके बाद की पंक्ति को प्रतिस्थापित करना चाहिए।
यह मेरा समाधान था।
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
valueएक चर?