0

I need to write a bash script that does the following:

  1. copies an html file
    • task: cp index.html index2.html
  2. replaces certain characters i.e. src="videos/ found inside of index2.html with new characters i.e. src="content://com.company.app/videos/
    • task: replace src="videos/ with src="content://com.company.app/videos/ inside of index2.html

As you can see, I can do step one. However, I need help doing step 2. What should I write in the bash script?

Dan Kreiger
  • 5,358
  • 2
  • 23
  • 27
  • [Obligatory](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) (You'll find things easier if you do the HTML editing in Perl/Python/JavaScript/etc after finding a decent DOM library). – Quentin Jul 03 '17 at 08:48

1 Answers1

1
sed -ri 's~(src=")(videos\/)~\1content\://com.company.app/\2~' index2.html

With sed you can enable regular expressions with -r and enable changes to index2.html with -i. Then we split src=" by surrounding it with brackets and do the same with videos\ We then refer to these as \1 and \2 adding the additional text. s signifies the sed substitute mode and so s~text in file~text to change to~

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Thanks @RamanSailopal - this works perfectly - I had to use `gsed` instead of `sed` to get the -r flag to work on my machine. But it worked great! Thanks. – Dan Kreiger Jul 03 '17 at 09:26