简体   繁体   English

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

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

In html file, I need to replace only the first occurrence of:在 html 文件中,我只需要替换第一次出现的:

<table id="any string" >

"any string" is whatever inside the " ". “任何字符串”是“”中的任何内容。 There is an space before the last > character.最后一个 > 字符之前有一个空格。

expected output:预期输出:

<table id="new string">

I know that maybe a sed -i can make it but i don't know how to match the "any string" part and only the first occurrence.我知道也许sed -i可以做到,但我不知道如何匹配“任何字符串”部分并且只匹配第一次出现。

It is possible.有可能的。 You're probably looking at something like this:你可能正在看这样的事情:

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

Assuming you any string actually means any string, as in you don't know what it is and it could be anything, you have to use the quotes as delimiters.假设您any string实际上意味着任何字符串,因为您不知道它是什么并且它可以是任何东西,您必须使用引号作为分隔符。 You mentioned sed so here's a simple sed solution:你提到了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

So it will match a table with an ID in double quotes, which includes a space, and replace everything in the ID up to that space with "new".因此,它将匹配一个带有双引号 ID 的表,其中包含一个空格,并将 ID 中直到该空格为止的所有内容替换为“new”。

Examples:例子:

<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" >

If any string actually means any string at all, not necessarily with a space (eg "foo"), and new string means any new string (eg bar), is the problem is a whole lot simpler:如果any string实际上意味着任何字符串,不一定带有空格(例如“foo”),并且new string意味着任何新字符串(例如 bar),那么问题就简单多了:

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

Examples:例子:

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

Using sed grouping and back referencing, you can exclude the text within the quotes as well as the space at the end.使用sed分组和反向引用,您可以排除引号内的文本以及末尾的空格。

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

This will create a backup of the original file.这将创建原始文件的备份。

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

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