繁体   English   中英

如何用shell脚本替换html文件中第一次出现的字符串

[英]How to replace first occurrence string in html file with shell script

在 html 文件中,我只需要替换第一次出现的:

<table id="any string" >

“任何字符串”是“”中的任何内容。 最后一个 > 字符之前有一个空格。

预期输出:

<table id="new string">

我知道也许sed -i可以做到,但我不知道如何匹配“任何字符串”部分并且只匹配第一次出现。

有可能的。 你可能正在看这样的事情:

#!/bin/bash
string='<table id="any string" >'
replace="new"
echo "${string/any/"replace"}"

假设您any string实际上意味着任何字符串,因为您不知道它是什么并且它可以是任何东西,您必须使用引号作为分隔符。 你提到了sed所以这是一个简单的sed解决方案:

# GNU sed needs -r for extended regexp, macOS sed needs -E for this
# s means for substitute
# / slashes are delimiters surrounding the paaterns, /before/after/
# [^ ] means any character that is *not* a space
# + means one or more of those characters
# followed by a space
# (.+) means one or more of any character, and remember what it is
# \1 use that first remembered pattern

sed -r 's/table id="[^ ]+ (.+)"/table id="new \1"/' file.html

因此,它将匹配一个带有双引号 ID 的表,其中包含一个空格,并将 ID 中直到该空格为止的所有内容替换为“new”。

例子:

<table id="any string" > -> <table id="new string" >
<table id="compact striped" > -> <table id="new striped" >
<table id="data compact striped" > -> <table id="new compact striped" >

如果any string实际上意味着任何字符串,不一定带有空格(例如“foo”),并且new string意味着任何新字符串(例如 bar),那么问题就简单多了:

sed -r 's/table id=".+"/table id="new"/' file.html

例子:

<table id="foo bar" > -> <table id="new" >
<table id="jabberwocky" > -> <table id="new" >

使用sed分组和反向引用,您可以排除引号内的文本以及末尾的空格。

$ sed -i.backup '0,/table id/s/\(table id="\).*\("\) /\1new string\2/' input_file
<table id="new string">

这将创建原始文件的备份。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM