How to replace any text between html tags

2019-09-20 06:20发布

问题:

i have text between html tags. For example:

<td>vip</td>

I will have any text between tags <td></td> How can i cut any text from these tags and put any text between these tags. I need to do it via bash/shell. How can i do this ? First of all, i tried to get this text, but without success sed -n "/<td>/,/<\/td>/p" test.txt. But in a result i have <td>vip</td>. but according to documentation, i should get only vip

回答1:

You can try this:

sed -i -e 's/\(<td>\).*\(<\/td>\)/<td>TEXT_TO_REPLACE_BY<\/td>/g' test.txt

Note that it will only work for the <td> tags. It will replace everything between tags <td> (actually with them together and put the tags back) with TEXT_TO_REPLACE_BY.



回答2:

You can use this to get the value vip

sed -e 's,.*<td>\([^<]*\)</td>.*,\1,g'


回答3:

If you Input_file is same as shown example then following may help you too.

echo "<td>vip</td>" | awk -F"[><]" '{print $3}'

Simply printing the tag with echo then using awk to create a field separator >< then printing the 3rd field then which is your request.



回答4:

d=$'<td>vip</td>\n<table>vip</table>\n<td>more data here</td>'
echo "$d"
<td>vip</td>
<table>vip</table>
<td>more data here</td> 

awk '/<td>/{match($0,/(<.*>)(.*)(<\/.*>)/,t);print t[1] "something" t[3];next}1' <<<"$d"
<td>something</td>
<table>vip</table>
<td>something</td>

awk '/<table>/{match($0,/(<.*>)(.*)(<\/.*>)/,t);print t[1] "something" t[3];next}1' <<<"$d"
<td>vip</td>
<table>something</table>
<td>more data here</td>