I'm trying to search and replace some values in my already created HTML template. Being a binary file, I've so far been unsuccessful in search and replacing my HTML.

I need to search for string 1111 and replace it with 1234 in here.

style='mso-bookmark:_MailOriginal'><span style='color:#1F497D'>1111</span><o:p></o:p></span></p>

Please suggest what command can be used as the HTML source code has plenty of HEX chars.

The HTML I want to to replace is https://pastebin.mozilla.org/8920460

  • What special characters? Why hasn't any answer been useful to you? If I remember correctly, which I usually do, there is nothing in that HTML that would prevent you from using any of the answers. Perhaps what you need is a proper HTML parses and something that operates on a tree structure rather than text? – Oskar Skog Feb 24 '17 at 6:17

You can also achieve it with a simple script writen in python:


f = open("index.html",'r') # open file with read permissions
filedata = f.read() # read contents
f.close() # closes file
filedata = filedata.replace("1111", "1234") # replace 1111 with 1234
filedata = filedata.replace("2222", "2345") # you can add as many replace rules as u need
f = open("index.html",'w') # open the same (or another) file with write permissions
f.write(filedata) # update it replacing the previous strings 
f.close() # closes the file

then run:

python replace.py
  • nope this didnt work with python. the script should handle all the special character for searching and replacing the html file. pastebin.mozilla.org/8920460 – scribbler Oct 20 '16 at 18:01
  • 1
    What special characters are you referring to? A HTML file is a text file, not a binary, so please give us a clue as to what is it that's not working. – schaiba Oct 20 '16 at 18:22
  • It treats html file as an binary file as it has /*.<> kind of special characters where shell has some special meaning for it. We can use \ as escape character but there are plenty of them, so searching for another way to do this. – scribbler Oct 20 '16 at 18:38

Sample file test.txt

should not touch 1111
should touch 1111
should not touch 1111

Using GNU Awk 3.1.7

awk 'BEGIN {s=0};{if (/<body/) {s=1;} else if (/<\/body>/) {s=0;};if (s) {gsub(1111,1234)}};1' test.txt


should not touch 1111
should touch 1234
should not touch 1111
  • this just works for the one line but I need such a code where in I could replace 1111 with 1234 in the html pastebin.mozilla.org/8920460 – scribbler Oct 20 '16 at 17:13
  • @scribbler so if I understand you correct. Would replacing 1111 with 1234 anywhere between the <body> tags work for you? – Zachary Brady Oct 20 '16 at 17:17
  • Yes that works for me.@zachary – scribbler Oct 20 '16 at 17:27
  • 1
    @scribbler updated answer for new requirements – Zachary Brady Oct 20 '16 at 18:25
  • 1
    @scribbler looks like it's not a plain <body>, I updated the answer. – Zachary Brady Oct 24 '16 at 18:33

sed(1) Stream EDitor is a good tool for (regex) search and replace.

Check man 1 sed

sed -e s/foo/bar/g infile > outfile

will replace everything that matches the regular expression "foo" with the substitution "bar".

PS. Use the -r flag if you need to use back-references in the substitution part.

  • No Oskar, that doesnt work as my html code has many special characters, I need something in my script wherein I can escape characters present in all the code. – scribbler Oct 20 '16 at 16:35
  • Can you give a more realistic example in your question? – Oskar Skog Oct 20 '16 at 16:37
  • pastebin.mozilla.org/8920460 – scribbler Oct 20 '16 at 17:11
  • above is the sample html in which I want to search and replace. – scribbler Oct 20 '16 at 17:12
  • I still don't see what a simple regex substitution would fail at here. And what do you mean by "binary" file? – Oskar Skog Oct 20 '16 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.