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

  • Home
  • SEARCH
  • 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 5997129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:13:08+00:00 2026-05-23T00:13:08+00:00

I have a very basic understanding of shell scripting, but what I need to

  • 0

I have a very basic understanding of shell scripting, but what I need to do requires more complex commands.

For one task, I need to find and replace html code within the index.html files on my server. These files are in multiple directories with a consistent naming convention. ([letter][3-digit number]) See the example below.

files: index.html
path: /www/mysite/board/today/[rsh][0-9]/ 
string to find: (div id="id")[code](/div)<--#include="(path)"-->(div id="id")[more code](/div)
string to replace with: (div id="id")<--include="(path)"-->(/div)

I hope you don’t mind the pseudo-regex. The folders containing my target index.html files look similar to r099, s017, h123. And suffice the say, the html code I’m trying to replace is relatively long, but its still just a string.

The second task is similar to the first, only the filename changes as well.

files: [rsh][0-9].html
path: www/mysite/person/[0-9]/[0-9]/[0-9]/card/2011/
string: (div id="id")[code](/div)<--include="(path)"-->(div id="id")[more code](/div)
string to replace with: (div id="id")<--include="(path)"-->(/div)

I’ve seen other examples on SO and elsewhere on the net that simply show scripts modifying files under a single directory to find & replace a string without any special characters, but I haven’t seen an example similar to what I’m trying to do just yet.

Any assistance would be greatly appreciated.

Thank You.

  • 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-05-23T00:13:09+00:00Added an answer on May 23, 2026 at 12:13 am

    You have three separate sub-problems:

    1. replacing text in a file
    2. coping with special characters
    3. selecting files to apply the transformation to

    ​1. The canonical text replacement tool is sed:

    sed -e 's/PATTERN/REPLACEMENT/g' <INPUT_FILE >OUTPUT_FILE
    

    If you have GNU sed (e.g. on Linux or Cygwin), pass -i to transform the file in place. You can act on more than one file in the same command line.

    sed -i -e 's/PATTERN/REPLACEMENT/g' FILE OTHER_FILE…
    

    If your sed doesn’t have the -i option, you need to write to a different file and move that into place afterwards. (This is what GNU sed does behind the scenes.)

    sed -e 's/PATTERN/REPLACEMENT/g' <FILE >FILE.tmp
    mv FILE.tmp FILE
    

    ​2. If you want to replace a literal string by a literal string, you need to prefix all special characters by a backslash. For sed patterns, the special characters are .\[^$* plus the separator for the s command (usually /). For sed replacement text, the special characters are \& and newlines. You can use sed to turn a string into a suitable pattern or replacement text.

    pattern=$(printf %s "$string_to_replace" | sed -e 's![.\[^$*/]!\\&!g')
    replacement=$(printf %s "$replacement_string" | sed -e 's![\&]!\\&!g')
    

    ​3. To act on multiple files directly in one or more directories, use shell wildcards. Your requirements don’t seem completely consistent; I think these are the patterns you’re looking for, but be sure to review them.

    /www/mysite/board/today/[rsh][0-9][0-9][0-9]/index.html
    /www/mysite/person/[0-9]/[0-9]/[0-9]/card/2011/[rsh][0-9].html
    

    This will match files like /www/mysite/board/today/r012/index.html and /www/mysite/person/4/5/6/card/2011/h7.html, but not /www/mysite/board/today/subdir/s012/index.html or /www/mysite/board/today/r1234/index.html.

    If you need to act on files in subdirectories recursively, use find. It doesn’t seem to be in your requirements and this answer is long enough already, so I’ll stop here.

    ​4. Putting it all together:

    string_to_replace='(div id="id")[code](/div)<--#include="(path)"-->(div id="id")[more code](/div)'
    replacement_string='(div id="id")<--include="(path)"-->(/div)'
    pattern=$(printf %s "$string_to_replace" | sed -e 's![.\[^$*/]!\\&!g')
    replacement=$(printf %s "$replacement_string" | sed -e 's![\&]!\\&!g')
    sed -i -e "s/$pattern/$replacement/g" \
      /www/mysite/board/today/[rsh][0-9][0-9][0-9]/index.html \
      /www/mysite/person/[0-9]/[0-9]/[0-9]/card/2011/[rsh][0-9].html
    

    Final note: you seem to be working on HTML with regular expressions. That’s often not a good idea.

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

Sidebar

Related Questions

I have a decent understanding of C# and a very basic understanding of powershell.
I have a very basic understanding of how autoconf and automake work, gathered from
I recently upgraded VS 2005 to VS 2008. Unfortunately I have a very basic
I am implementing a very simple file database. I have 2 basic operations: void
We have very strange problem, one of our applications is continually querying server by
i have very simple problem. I need to create model, that represent element of
I have a very simple problem which requires a very quick and simple solution
i have a decent understanding of configuring drupal and using modules for basic stuff,
I have a very basic doubt regarding cloud computing that is catching up pretty
I have a very basic LEFT OUTER JOIN to return all results from the

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.