This should work in many situations, we replace Hello with OtherWord using awk's general substitution function.
Code: Select all
awk '/<p>/{f=1} f==1{gsub(/Hello/,"OtherWord"); print $0} /<\/p>/{f=0}' filename.html > edited_file.html
f=1 and f=0 are 'on and off switches' we create to differentiate the lines where we want to apply substitutions, there could be a word Hello incrusted in a <H1></H1> tag and perhaps we do not want a substitution there.
In its basic form awk reads line by line a stream and tries to find a pattern to then execute some action or actions, you can have more than one pattern/action, so in the code snippet above we have three pattern/actions:
1st. /<p>/{f=1}, scan for the string "<p>", if you find it, then create and assign f=1
2nd. f==1{gsub(/Hello/,"OtherWord"); print $0}, if the value of f is 1 then substitute Hello for OtherWord, and then print the line
3rd. /<\/p>/{f=0}, scan for the string "</p>", if you find set variable f to 0, f=0
Further reading: https://www.gnu.org/software/gawk/manual/gawk.html
results from terminal,
Code: Select all
root# echo '"htmlContent":"<html><head></head><body><p>Hello,</p><p>Hello</p></body></html>"' | awk '/<p>/{f=1} f==1{gsub(/Hello/,"OtherWord"); print $0} /<\/p>/{f=0}'
"htmlContent":"<html><head></head><body><p>OtherWord,</p><p>OtherWord</p></body></html>"