Rhys
Rhys

Reputation: 2877

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'

Upvotes: 1

Views: 198

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 360315

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;
}

Upvotes: 1

cdarke
cdarke

Reputation: 44394

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.

Upvotes: 0

Bram
Bram

Reputation: 825

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

Upvotes: 0

Related Questions