简体   繁体   中英

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

In html file, I need to replace only the first occurrence of:

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

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. You mentioned sed so here's a simple sed solution:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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