Friday, 14 December 2018

regex - sed match pattern and substitute in html



I want to substitute text in-between only specific html tag, example html file






original text
original text
original text


original text





Here I want to replace text "original" by "modified" only in-between the tag "title" I tried the following but that replaces everywhere except before title tag. I do need to keep global "g".



 sed -i '' '//,/<\/title>/ s/original/modified/g' test.html<br/></code></pre><br/><br/><p>Also, I don't understand why do I need ''(two single quote) in my script, for example this one works well (with double quotes)</p><br/><br/><pre><code> sed -i -e "s/original/modified/g" test.html<br/></code></pre><br/><br/><br/><p>if I have match pattern then the below one doesn't work</p><br/><br/><pre><code> sed -i -e "/first/,/last/ s/original/modified/g" test.html<br/></code></pre><br/><br/><p>and I need to have '' to get it work as below</p><br/><br/><pre><code>  sed -i '' '/first/,/last/ s/original/modified/' test.html<br/></code></pre><br/><br/><br/><p>I am using Mac OSX 10.9, also not sure of a better way to do this. thanks</p><br/>    </div><div class="post-text" itemprop="text"> <div style="font-weight: bold;"><p class="normal">Answer</p> <br/></div><br/><p>First '' is due to option -i</p><br/><br/><blockquote><br/>  <p><code>-i[SUFFIX]'</code>--in-place[=SUFFIX]'<br/>       This option specifies that files are to be edited in-place.  GNU<br/>       `sed' does this by creating a temporary file and sending output to<br/>       this file rather than to the standard output.(1).</p><br/><br/></blockquote><br/><br/><p>sed work line by line by default and pattern address (/start/,/end/) define the line to start until which OTHER line containing the end parttern so in your case where  is on the same line, sed start at  and stop at the end of file, not like expected</p><br/><br/><p>assuming the are always on the same line in your case</p><br/><br/><pre><code>sed -i '' '/<title>/ {<br/>: loop<br/>   s|\(<title>.*\)original\(.*\)|\1modified\2|
t loop

}' test.html

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...