0

I am using WGET to burn a static copy of a PHP website. I would like to remove all references to .html in every <a href for every file. So any link for example, <a href="path/project-name.html">Project Name</a>, I would like changed to <a href="path/project-name">Project Name</a>.

The command grep -rl index.html . | xargs sed -i 's/index.html//g' works great at removing every index.html in all the links.

But I can't get it to work for every .html link with the command grep -rl *.html . | xargs sed -i 's/*.html//g'.

Any assistance with my regex would be much appreciated.

3
  • 1
    's/*.html//g' is wrong since you entered a glob pattern in the LHS, where a regex pattern is expected. What is the pattern you want to match? Jul 25, 2021 at 14:36
  • Thanks, @wiktor. So any link for example, <a href="path/project-name.html">Project Name</a>, I would like changed to <a href="path/project-name">Project Name</a>. Jul 25, 2021 at 14:41
  • 1
    Try grep -rl *.html . | xargs sed -i -E 's/(href="[^"]*)\.html"/\1"/g' Jul 25, 2021 at 14:43

1 Answer 1

1

's/*.html//g' is wrong since you entered a glob pattern in the LHS (left hand side part of the sed substitution command), where a regex pattern is expected.

You can use

grep -rl *.html . | xargs sed -i -E 's/(href="[^"]*)\.html"/\1"/g'

Details:

  • -E - the option that enables the POSIX ERE regex syntax
  • (href="[^"]*)\.html" - matches and captures into Group 1 (later, accessed via \1 backreference) a href=" substring and any zero or more chars other than " after, and then just matches .html substring
  • \1" - replaces with Group 1 and a " char
  • g - all non-overlapping occurrences on a line.
2

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.