0

I have a few directories, all of which have it's own index.html file inside. I want to scan these directories (I don't know how much of them will it be and how named) and replace one line in all of the index.html files they contain.

The line I look for is base href=". I want to put a given text at the end of this string. For example the text may be: /testing/demo. Then I want all the index.html files to contain base href="/testing/demo.

Also, two additional steps must be made - if there's a slash / at the end of the given string, it should delete it. If there's not a slash at the beginning, it should add it.

I tried to use sed comand somehow.

6
  • 1
    can you add a full example how its look like and how you expect it ? Commented Mar 14, 2019 at 13:43
  • It doesn't exist yet, I want to add it to my deploy script. I thought about something like: for d in $(find ${S3_URI} -type d); do find $d -iname "index.html" ............. Commented Mar 14, 2019 at 13:45
  • And then use the sed command to replace the strings. Commented Mar 14, 2019 at 13:46
  • wrt It doesn't exist yet - we're asking you to create and post an example. See How to Ask. Commented Mar 14, 2019 at 17:10
  • @EdMorton when ask 'how to write something' how can I provide an example of it? I was just looking for some solutions. In the comment above I wrote how I wanted to approach the problem. Commented Mar 19, 2019 at 15:06

1 Answer 1

2

You can use the following command to find all index.html files and add string after base href=":

Dont forget to add -i as an option to sed to make it modify in place

The -E for extend regex usage as described in here:

-E, -r, --regexp-extended use extended regular expressions in the script (for portability use POSIX -E).

We start by looking for each index.html file regardless the case sensitivity. and then we use sed to find base href= and capture it inside a group then we pass it to the second part of the sed command as \1 after that we add the string we want to append.

For the second part of the question we can consider removing any / at the start of our string and also remove any / at the end and save the result in a variable which will be passed to sed

PARSED_URL=$(echo '/testing/demo/' | grep -oP '[^/].*[^/$]') ; find /path/you/want/to/lookup/ -iname "index.html" -exec sed -E "s~(base\shref=\")~\1/$PARSED_URL~g" {} \; 

Input 1:

/testing/demo/ 

Input 2:

testing/demo/ 

Input 3:

/testing/demo 

Input 4:

testing/demo 

Output (after appending it to base href=")

base href="/testing/demo 
Sign up to request clarification or add additional context in comments.

7 Comments

What does -E flag mean here?
answer updated. if there is any other modification needed let me now so i can keep the answer updated
It's a very nice anwser but lacks one aspect of my question: if there's a slash / at the end of the given string, it should delete it. If there's not a slash at the beginning, it should add it.
It's working fine, I get the updated index.html file in the console, but the file is not saved with the new content. How could I achieve this last step? :)
This is because you need to add -i to sed command, I just did not want to add it so you can check the result first
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.