简体   繁体   English

sed commdand-多次出现字符串时,查找和替换字符串模式不起作用

[英]Sed commdand - find and replace string pattern is not working when string occurs multiple times

I am not an expert in UNIX commands but trying to create one complex replacement by finding and replacing for XML files under a directory. 我不是UNIX命令方面的专家,但是试图通过查找和替换目录下的XML文件来创建一个复杂的替换。

Here is my file structure : 这是我的文件结构:

<LocaleCountry>
<link href="../Countries/AZ.xml" path="/Config/Countries/AZ"/>
<link href="../Countries/AM.xml" path="/Config/Countries/AM"/>
</LocaleCountry>

What I am trying to do make the replacement on two patterns, by usage of command, sed is necessary as our server only has sed as a proper command ... 我试图通过使用命令在两种模式下进行替换,因为我们的服务器仅将sed作为适当的命令使用,所以sed是必需的...

Transforming 转型

from <link href="../Countries/ to <Country file="
from path="/Config/Countries/ to code="

So in the end my output should look like : 因此,最终我的输出应如下所示:

<LocaleCountry>
<Country file="AZ.xml" code="AZ"/>
<Country file="AM.xml" code="AM"/>
</LocaleCountry>

What I have tried works, but not multiple "link" elements, it only changes the first one and won't touch the others, most probably I couldn't create a correct expression for my pattern ... 我尝试过的方法有效,但不能使用多个“链接”元素,它只会更改第一个元素,而不会涉及其他元素,很可能我无法为我的模式创建正确的表达式...

Here is what I have tried: 这是我尝试过的:

sed -i 's#<link href="../Countries/#<Country file="#' *.xml
sed -i 's#path="/Config/Countries/#code="#' *.xml

Can you please check what I'm doing wrong and suggest a solution if possible? 您能否检查我做错了什么,并在可能的情况下提出解决方案?

Just tell the substitution command to make it global: 只需告诉替代命令使其全局即可:

sed -i 's#<link href="../Countries/#<Country file="#g' *.xml
sed -i 's#path="/Config/Countries/#code="#g' *.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM