2016-10-19 279 views
0

中出現的所有我想替換字符串的所有出現:替換字符串

https://support.office.com/en-us/article/Check-for-accessibility-issues-a16f6de0-2f39-4a2b-8bd8-5ad801426c7f?ui=en-US&rs=en-US&ad=US 

xyz。這是爲當前目錄下的所有文件完成的。

我用下面的命令:

find . -type f | xargs sed -i 's/https\:\/\/support\.office\.com\/en\-us\/article\/Check\-for\-accessibility\-issues\-a16f6de0\-2f39\-4a2b\-8bd8\-5ad801426c7f\?ui\=en\-US\&rs\=en\-US\&ad\=US/xyz/g'; 

但什麼也沒做。我認爲我逃避特殊角色的方式是錯誤的。我如何糾正它?

現在,我在\前加上每個非字母數字字符。

回答

1

SED默認使用BRE,你躲過一些字符,這給了它特殊的含義BRE,例如,\?並不意味着字面?,相反,它意味着,zero or one occurrence

你應該調整你的逃跑..或在sed中使用除/以外的分隔符。

...sed '[email protected]://[email protected]@g` 
+0

@anukul你應該看看什麼是BRE中的特殊字符。我想在你的例子中,你只需要逃避'.'(dot) – Kent

+0

好吧,如何改變分隔符有幫助? – anukul

+0

@anukul我回答的最後一行代碼就是例子,我選了'@'。它是默認的'/',你必須避開所有的斜槓,我認爲你有很多的網址。 – Kent

2

您可以使用另一個分隔符,如~,並避免轉義sed命令中的大部分符號。

試試這個命令:

find . -type f -exec sed -i 's~https://support\.office\.com/en-us/article/Check-for-accessibility-issues-a16f6de0-2f39-4a2b-8bd8-5ad801426c7f?ui=en-US&rs=en-US&ad=US~xyz~g' '{}' + 

即使?不需要在BRE進行轉義,因爲它字面?匹配。

+0

好的,這工作。但是現在我需要逃脫哪些角色? – anukul

+0

你需要轉義'[*。 ]^$'等等 – anubhava

1

如果perl是好的,檢查出quotemeta

$ cat ip.txt 
https://support.office.com/en-us/article/Check-for-accessibility-issues-a16f6de0-2f39-4a2b-8bd8-5ad801426c7f?ui=en-US&rs=en-US&ad=US 

$ perl -pe 's|\Qhttps://support.office.com/en-us/article/Check-for-accessibility-issues-a16f6de0-2f39-4a2b-8bd8-5ad801426c7f?ui=en-US&rs=en-US&ad=US|xyz|' ip.txt 
xyz 


並使用exec選項,而不是xargs的

find -type f -exec perl -i -pe 'print "$ARGV" if s|\Qhttps://support.office.com/en-us/article/Check-for-accessibility-issues-a16f6de0-2f39-4a2b-8bd8-5ad801426c7f?ui=en-US&rs=en-US&ad=US|xyz|' {} + 
+0

如果'|'是URL的一部分,該怎麼辦? – anukul

+0

你必須逃避它。'echo'as | asd'| perl -pe's | \ Qs \ | a | xyz |'' – Sundeep

0

嘗試它,它沒有工作

SED的@的wp-content /插件S/BWP-縮小/分鐘/ΔF=可溼性粉劑內容/插件/共享這個/ CSS/style.css中,可溼性粉劑內容/插件/ WP到微博/ CSS/Twitter的feed.css,可溼性粉劑內容/themes/yourgarden/style.css,wp-content/plugins/cleverness-to-do-list/css/cleverness-to-do-list-frontend.css,wp-content/plugins/cleverness-to-do-list /css/jquery-ui-fresh.css?wp-content/css/[email protected]' sed:-e表達式#1,字符344:未終止的's'命令