我有这个xml数据
<code>
我需要使用<code_>
更改<place><name>
,<place><name_>
更改sed 's/<institution>.*<code>.*<\/code>/<institution>.*<code_>.*<\/code_>/g'
。如何用sed和regex完成这项工作?
我尝试使用const fs = require('fs');
const path = require('path');
// Folder where all your individual Cloud Functions files are located.
const FUNCTIONS_FOLDER = './scFunctions';
fs.readdirSync(path.resolve(__dirname, FUNCTIONS_FOLDER)).forEach(file => { // list files in the folder.
if(file.endsWith('.js')) {
const fileBaseName = file.slice(0, -3); // Remove the '.js' extension
const thisFunction = require(`${FUNCTIONS_FOLDER}/${fileBaseName}`);
for(var i in thisFunction) {
exports[i] = thisFunction[i];
}
}
});
,但替换字符串上的。*变为。*不是任何与正则表达式匹配的字符串。
答案 0 :(得分:0)
这里的主要问题是不使用XML / HTML解析器,而在处理XML / HTML数据时总是应该使用它们:
使用xmlstarlet
工具的正确方法:
xmlstarlet ed -O -r '//institution/code' -v 'code_' -r '//place/name' -v 'name_' input.xml
输出:
<institution>
<id>83812745840</id>
<code_>2701811200</code_>
<full_name>full name 1</full_name>
<address>adress 1</address>
<institution_type>
<id>191</id>
<code>inst code 1</code>
<name>institution name1</name>
</institution_type>
<place>
<id>812007638</id>
<name_>place-name_1</name_>
<code>415995</code>
</place>
<activity>
<code>811855905</code>
<name>act-name-1</name>
<equipment_specialty>false</equipment_specialty>
</activity>
</institution>
要在地方修改文件,请添加-L
选项:xmlstarlet ed -O -L ....