使用sed替换HTML文件中的变量文本

时间:2022-05-13 16:51:44

I'm going crazy trying to follow the sed BRE (Basic Regular Expression) rules to replace a pretty simple occurrence in an HTML file.

我疯狂地尝试遵循sed BRE(基本正则表达式)规则来替换HTML文件中非常简单的事件。

I think I'm following the spec correctly, but none of my attempts are working.

我认为我正确地遵循了规范,但我的尝试都没有奏效。

What I'd like to do is replace the contents of one meta tag with something new (which I'm calculating as part of the script I'm writing). Basically, the meta tag looks like this:

我想做的是用一些新东西替换一个元标记的内容(我正在计算作为我正在编写的脚本的一部分)。基本上,元标记看起来像这样:

<meta version="v0.103.2" />

It always contains an attribute which follows that syntax, which in a JS regular expression could be characterized as:

它总是包含一个遵循该语法的属性,在JS正则表达式中可以将其表征为:

/version=\"v[0-9]+\.[0-9]+\.[0-9]+\"/g

So I've tried a whole bunch of sed commands to get this working, and it just doesn't seem to work as documented, unless I'm misunderstanding things. Some of the commands I've tried:

所以我尝试了一大堆sed命令来实现这个功能,它似乎没有记录,除非我误解了一些事情。我试过的一些命令:

I'm on a Mac (which matters because of sed syntax changes between Unix/Linux), and this accounts for the '' in the commands below.

我在Mac上(因为Unix / Linux之间的sed语法更改很重要),这在下面的命令中说明了''。

✗ sed -i -E '' s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\"whatever/g ./src/index.html
✗ sed -i -E '' s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\"whatever/g './src/index.html'
✗ sed -i -E s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\"whatever/g ./src/index.html
✗ sed -i -E s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\whatever/g ./src/index.html
✗ sed -i -E s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\"whatever/g ./src/index.html
✗ sed -i '' s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\"whatever/g ./src/index.html 
✗ sed -i '' s/version=\"v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/version=\whatever/g ./src/index.html 
✗ sed -i '' s/version=\"v[:digit:]+\.[:digit:]+\.[:digit:]+/version=\whatever/g ./src/index.html 
✗ sed -i '' s/v[:digit:]+\.[:digit:]+\.[:digit:]+/whatever/g ./src/index.html 
✗ sed -i '' s/v[:digit:]+[:punct:][:digit:]+[:punct:][:digit:]+/whatever/g ./src/index.html
✗ sed -i '' s/v[:digit:][:punct:][:digit:][:digit:][:digit:][:punct:][:digit:]+/whatever/g ./src/index.html 
✗ sed -i '' s/v[:digit:][:punct:][:digit:][:digit:][:digit:][:punct:][:digit:]/whatever/g ./src/index.html
✗ sed -i '' s/v[0-9]+\.[0-9]+.[0-9]+/whatever/g ./src/index.html
✗ sed -i '' s/version=\"[^ \"]+\"/whatever/g ./src/index.html
✗ sed -i '' s/version="[^ "]+"/whatever/g ./src/index.html 
✗ sed -i '' s/version=\"[^\"]+\"/whatever/g ./src/index.html 
✗ sed -i '' s/version=/whatever=/g ./src/index.html
✗ sed -i '' s/version=\"/whatever=\'/g ./src/index.html
✗ sed -i '' s/version=\"[^\"]/whatever=\"/g ./src/index.html

Help? It is finding the file correctly. When I try versions of this that don't include a regex (see third from the bottom), it replaces correctly, but somehow my regex syntax seems not to be working here.

帮帮我?它正确地找到了文件。当我尝试不包含正则表达式的版本(从底部看第三个)时,它会正确替换,但不知怎的,我的正则表达式语法似乎没有在这里工作。

Or can anyone think of an easier/more reliable way to do this?

或者,任何人都可以想到更简单/更可靠的方法吗?

1 个解决方案

#1


2  

You need to quote the regex and instead of + (that is treated as a literal + by the BRE POSIX) use either * (that matches 0 or more occurrences) or \{1,\} range quantifier (in BRE POSIX, the {n,m} must be written as \{m,n\}) that matches 1 or more occurrences.

你需要引用正则表达式而不是+(被BRE POSIX视为文字+)使用*(匹配0或更多次出现)或\ {1,\}范围量词(在BRE POSIX中,{ n,m}必须写为匹配1个或多个匹配项的\ {m,n \})。

Thus, you may use

因此,您可以使用

sed -i '' "s/version=\"v[0-9]*\.[0-9]*\.[0-9]*\"/whatever=/g" ./src/index.html

With single quotes:

单引号:

sed -i '' 's/version="v[0-9]*\.[0-9]*\.[0-9]*"/whatever=/g' ./src/index.html

#1


2  

You need to quote the regex and instead of + (that is treated as a literal + by the BRE POSIX) use either * (that matches 0 or more occurrences) or \{1,\} range quantifier (in BRE POSIX, the {n,m} must be written as \{m,n\}) that matches 1 or more occurrences.

你需要引用正则表达式而不是+(被BRE POSIX视为文字+)使用*(匹配0或更多次出现)或\ {1,\}范围量词(在BRE POSIX中,{ n,m}必须写为匹配1个或多个匹配项的\ {m,n \})。

Thus, you may use

因此,您可以使用

sed -i '' "s/version=\"v[0-9]*\.[0-9]*\.[0-9]*\"/whatever=/g" ./src/index.html

With single quotes:

单引号:

sed -i '' 's/version="v[0-9]*\.[0-9]*\.[0-9]*"/whatever=/g' ./src/index.html