[英]Using find/sed to replace strings in text files- works only on some of the matches
我想換
{not STRING }
和
(not STRING )
我跑了
尋找。 -maxdepth 1 -type f -exec sed -i -E 's/{not\s([^\s}]+)\s}/(not \1 )/g' {};
它在一些比賽中起作用。 當我使用相同的模式運行grep
時,它會顯示更多仍然具有STRING
的文件。 再次運行find/sed
,結果相同。
您需要轉義大括號 ( {}
),因為它們是正則表達式元字符。 \s
也不是 POSIX sed,我會使用更便攜的[[:space:]]
。
您的代碼不適用於我的示例文本(GNU/Linux)。 這樣做:
sed -E 's/\{not[[:space:]]+([^[:space:]}]+)[[:space:]]+\}/(not \1 )/g'
我還允許在not
之后和}
之前直接使用可變長度空格(使用[[:space:]]+
)。 你可能想要也可能不想要。
還:
-i
提供后綴參數。;
對於 find -exec
必須加引號 ( \;
) 以避免被 shell 解釋。所以命令是:
find . -maxdepth 1 -type f -exec \
sed -E -i .TMP 's/\{not[[:space:]]+([^[:space:]}]+)[[:space:]]+\}/(not \1 )/g' {} \;
如果.TMP
與現有文件沖突,請選擇不同的后綴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.