简体   繁体   中英

Changing <p> html via shell script

I have the below html and I would like to change the change the <p> Text </p> section based on a var in my shell script.

<div id="demo"><p> Text </p></div>

I have been attempting to use the below function, but it doesn´t seem to work, Also I need it to pick up change whatever is in the <p> Text </p> section as it will change regularly

fnChangeTxt()
{
        sed -i 's/<div id="'$1'"><p>*.*</<div id="'$1'"><p>'$2'</' /var/www/html/alarm.html
}

Calling the function

fnChangeTxt 'demo' 'Next'

You need to change your quoting. For readability, I'd suggest using a different delimiter for the s command - one that is less likely to appear in the text you're modifying. That will make it less likely for a reader to be confused by seeing slashes in odd places in something that's working with HTML.

fnChangeTxt() 
{
     sed -i "s|<div id=\"$1\"><p>.*<|<div id=\"$1\"><p>$2<|" /var/www/html/alarm.html;
}

For starters the single quotes will prevent replacing the variables with the provided input.

Secondly you need only .* to match "everything". (acually "0 ro more occurences of any character")

No need for sed really:

#!/bin/bash

new='some stuff'

while read
do
    echo ${REPLY/<p>*<\/p>/<p> $new </p>}

done << '__END__'
<div id="demo"><p> Text </p></div> 
<div id="demo"><p> Text </p></div> 
__END__

Gives:

<div id="demo"><p> some stuff </p></div>
<div id="demo"><p> some stuff </p></div>

Replace the here document with your filename.

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