[英]Using sed awk to replace characters in a string
我的一個文件夾中有大約300個文件。 我需要在文件中搜索該標簽
<b>This string must stay</b><br />
並替換為
<CustomerInfo>This string must stay</CustomerInfo>
內部字符串不得更改。
嘗試這個 :
sed 's@<b>.*</b><br />@<CustomerInfo>This string must stay</CustomerInfo>@g' file.txt
這將替換當前目錄和子目錄中每個文件中的字符串(它不會忽略二進制文件,因此請小心使用)
這適用於命令依賴中的查找(unix具有)
首先要確保這不會破壞任何內容,請嘗試以下操作(它只會顯示其功能的預覽):
find . -type f -exec sed -e 's@<b>\(.*\)</b><br />@<CustomerInfo>\1</CustomerInfo>@g' {} \;
如果它按預期工作了,那就來龍了:
find . -type f -exec sed -i 's@<b>\(.*\)</b><br />@<CustomerInfo>\1</CustomerInfo>@g' {} \;
您可以使用以下內容:
sed -r 's#<b>([^<]*)</b><br />#<CustomerInfo>\1</CustomerInfo>#g' file
確定可以使用后,將-r
替換為-ir
以進行就地編輯。
如果要通過文件運行它,請在某處添加注釋,請執行以下操作:
find $1 -type f -iname '*.html' -exec \
sed -r 's#<b>([^<]*)</b><br />#<CustomerInfo>\1</CustomerInfo>#g' {} \;
<b>([^<]*)</b><br />
尋找具有<b>....</b><br />
並在中間<b>....</b><br />
文本。 <CustomerInfo>\\1</CustomerInfo>
替換為<CustomerPage>
+給定文本+ </CustomerPage>
。 給定一個示例文件:
$ cat a
hello
bye
aaa <b>hello</b><br /> beee
<b>hello</b><br />
<b>hello</b>blabla
讓我們嘗試一下:
$ sed -r 's#<b>([^<]*)</b><br />#<CustomerInfo>\1</CustomerInfo>#g' a hello
bye
aaa <CustomerInfo>hello</CustomerInfo> beee
<CustomerInfo>hello</CustomerInfo>
<b>hello</b>blabla
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.