Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:29:10+00:00 2026-06-04T07:29:10+00:00

I have about 150 HTML files in a given directory that I’d like to

  • 0

I have about 150 HTML files in a given directory that I’d like to make some changes to. Some of the anchor tags have an href along the following lines: index.php?page=something. I’d like all of those to be changed to something.html. Simple regex, simple script. I can’t seem to get it correct, though. Can somebody weigh in on what I’m doing wrong?

Sample html, before and after output:

<!-- Before -->
<ul>
    <li><a href="#">Apple</a></li>
    <li><a href="index.php?page=dandelion">Dandelion</a></li>
    <li><a href="index.php?page=elephant">Elephant</a></li>
    <li><a href="index.php?page=resonate">Resonate</a></li>
</ul>

<!-- After -->
<ul>
    <li><a href="#">Apple</a></li>
    <li><a href="dandelion.html">Dandelion</a></li>
    <li><a href="elephant.html">Elephant</a></li>
    <li><a href="resonate.html">Resonate</a></li>
</ul>

Script file:

#! /bin/bash

for f in *.html
do
    sed s/\"index\.php?page=\([.]*\)\"/\1\.html/g < $f >! $f
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:29:12+00:00Added an answer on June 4, 2026 at 7:29 am

    It’s your regex, and the fact that the shell is trying to interpret bits of your regex.

    First – the [.]* matches any number of literal dots .. Change it to .*.

    Secondly, enclose the entire regex in single quotes ' to prevent the bash shell from interpreting any of it.

    sed 's/"index\.php?page=\(.*\)"/\1\.html/g'
    

    Also, instead of < $f >! $f you can just feed in the ‘-i’ switch to sed to have it operate in-place:

    sed -i 's/"index\.php?page=\(.*\)"/"\1\.html"/g' "$f"
    

    (Also, as another point I think in your replacement you want double quotes around the \1.html so that the new URL is quoted within the HTML. I also quoted your $f to "$f", because if the file name contains spaces bash will complain).

    EDIT: as @TimPote notes, the standard way to match something within quotes is either ".*?" (so that the .* is non-greedy) or "[^"]+". Sed doesn’t support the former, so try:

    sed -i 's/"index\.php?page=\([^"]\+\)"/"\1\.html"/g' "$f"
    

    This is to prevent (for example) <a href="index.php?page=asdf">"asdf"</a> from being turned into <a href="asdf">"asdf.html"</a> (where the (.*) captured asdf">"asdf, being greedy).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single crystal report that usually ends up about 150 pages long.
I have about 150 text files filled with character information. Each file contains two
i have about 140 - 150 stored procedures that I need to migrate from
I have about 10 drop down list controls that get populated. Instead of copying/pasting
I have about 4-5 years of background in programming some in C# and some
We have about 7 app servers running .NET windows services that ping a single
I have about twenty UITextViews and corresponding .txt files. What I want is to
I have about 15 .RPT files on my .NET 2.0 site, and I recently
I have two resize functions that I would like to perform one after the
I have a form with about 150 fields, however the user can add more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.