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 6242847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:01:49+00:00 2026-05-24T12:01:49+00:00

I have a file that has over 200 lines in this format: name old_id

  • 0

I have a file that has over 200 lines in this format:

name old_id new_id

The name is useless for what I’m trying to do currently, but I still want it there because it may become useful for debugging later.

Now I need to go through every file in a folder and find all the instances of old_id and replace them with new_id. The files I’m scanning are code files that could be thousands of lines long. I need to scan every file with each of the 200+ ids that I have, because some may be used in more than one file, and multiple times per file.

What is the best way to go about doing this? So far I’ve been creating python scripts to figure out the list of old ids and new ids and which ones match up with each other, but I’ve been doing it very inefficient because I basically scanned the first file line by line and got the current id of the current line, then I would scan the second file line by line until I found a match. Then I did this over again for each line in the first file, which ended up with my reading the second file a lot. I didn’t mind doing this inefficiently because they were small files.

Now that I’m searching probably somewhere around 30-50 files that can have thousands of line of code in it, I want it to be a little more efficient. This is just a hobbyist project, so it doesn’t need to be super good, I just don’t want it to take more than 5 minutes to find and replace everything, then look at the result and see that I made a little mistake and need to do it all over again. Taking a few minutes is fine(although I’m sure with computers nowadays they can do this almost instantly still) but I just don’t want it to be ridiculous.

So what’s the best way to go about doing this? So far I’ve been using python but it doesn’t need to be a python script. I don’t care about elegance in the code or way I do it or anything, I just want an easy way to replace all of my old ids with my new ids using whatever tool is easiest to use or implement.

Examples:

Here is a line from the list of ids. The first part is the name and can be ignored, the second part is the old id, and the third part is the new id that needs to replace the old id.

unlock_music_play_grid_thumb_01 0x108043c 0x10804f0

Here is an example line in one of the files to be replaced:

const v1, 0x108043c

I need to be able to replace that id with the new id so it looks like this:

const v1, 0x10804f0
  • 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-24T12:01:51+00:00Added an answer on May 24, 2026 at 12:01 pm

    Use something like multiwordReplace (I’ve edited it for your situation) with mmap.

    import os
    import os.path
    import re
    from mmap import mmap
    from contextlib import closing
    
    
    id_filename = 'path/to/id/file'
    directory_name = 'directory/to/replace/in'
    
    # read the ids into a dictionary mapping old to new
    with open(id_filename) as id_file:
        ids = dict(line.split()[1:] for line in id_file)    
    
    # compile a regex to do the replacement
    id_regex = re.compile('|'.join(map(re.escape, ids)))
    
    def translate(match):
        return ids[match.group(0)]
    
    def multiwordReplace(text):
        return id_regex.sub(translate, text)
    
    for code_filename in os.listdir(directory_name):
        with open(os.path.join(directory, code_filename), 'r+') as code_file:
            with closing(mmap(code_file.fileno(), 0)) as code_map:
                new_file = multiword_replace(code_map)
        with open(os.path.join(directory, code_filename), 'w') as code_file:
            code_file.write(new_file)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tab-delimited file that has over 200 million lines. What's the fastest
So I have this file that has an exclamation mark over it, so I
I have a test file that has many lines, each line looks something like:
I have an external JSON file that has this in it: { Home: [
We have a file that has a 64 bit integer as a string in
I have a file that has one entry per line. Each line has the
I have a HTML file that has code similar to the following. <table> <tr>
I have a jar file that has a file named client.ts in (when viewing
I have an Excel file that has a bunch of VBA and macro code
I have a CSV file that has only 1 column, but has close to

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.