簡體   English   中英

sed commdand-多次出現字符串時,查找和替換字符串模式不起作用

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

我不是UNIX命令方面的專家,但是試圖通過查找和替換目錄下的XML文件來創建一個復雜的替換。

這是我的文件結構:

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

我試圖通過使用命令在兩種模式下進行替換,因為我們的服務器僅將sed作為適當的命令使用,所以sed是必需的...

轉型

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

因此,最終我的輸出應如下所示:

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

我嘗試過的方法有效,但不能使用多個“鏈接”元素,它只會更改第一個元素,而不會涉及其他元素,很可能我無法為我的模式創建正確的表達式...

這是我嘗試過的:

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

您能否檢查我做錯了什么,並在可能的情況下提出解決方案?

只需告訴替代命令使其全局即可:

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