[英]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.