[英]sed command to replace string
我正在嘗試將%2 $ u替換為
<ph name='NUMBER' ex='%2$u'/>
使用以下命令跨多個文件。
find . -name "*.txt" -print | xargs sed -i 's/%2$u/<ph name='NUMBER' ex='%2$u'\/>/g'
實際上%2 $ u會像這樣被替換
<ph name=NUMBER ex=%2/>
有人可以給我解決方案嗎? 提前致謝。
-蘭吉特
您不能在單引號字符串內嵌入單引號。 嘗試:
find . -name "*.txt" -print |
xargs sed -i 's/%2$u/<ph name='"'"'NUMBER'"'"' ex='"'"'%2$u'"'"'\/>/g'
要么
find . -name "*.txt" -print |
xargs sed -i "s/%2\$u/<ph name='NUMBER' ex='%2\$u'\/>/g"
根據sed的版本,您可能需要轉義sed的$,以防止它僅匹配行尾:
xargs sed -i "s/%2\\\$u/<ph name='NUMBER' ex='%2\\\$u'\/>/g"
用單引號引起來時 ,下一個單引號結束引號。 因此,后者實際上包括以下部分:
's/%2$u/<ph name='
NUMBER
' ex='
%2$u
'\/>/g'
在未報價的零件中,發生參數擴展 。 因此$u
將被參數u
的值或空字符串(如果不存在)替換。 您可以使用簡單的echo
進行測試:
echo 's/%2$u/<ph name='NUMBER' ex='%2$u'\/>/g'
為了避免這種情況,請使用不同的報價技術,例如,對包含單引號的部分使用雙引號(請記住在其中轉義$
,否則會發生擴展):
's/%2$u/<ph name='"'NUMBER'"' ex='"'%2\$u'"'\/>/g'
或在替換的字符串中使用雙引號(如果適用):
's/%2$u/<ph name="NUMBER" ex="%2$u"\/>/g'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.