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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:10:09+00:00 2026-05-27T03:10:09+00:00

I frequently need to make many replacements within files. To solve this problem, I

  • 0

I frequently need to make many replacements within files. To solve this problem, I have created two files old.text and new.text. The first contains a list of words which must be found. The second contains the list of words which should replace those.

  • All of my files use UTF-8 and make use of various languages.

I have built this script, which I hoped could do the replacement. First, it reads old.text one line at a time, then replaces the words at that line in input.txt with the corresponding words from the new.text file.

#!/bin/sh
number=1
while read linefromoldwords
do
    echo $linefromoldwords
    linefromnewwords=$(sed -n '$numberp' new.text)
    awk '{gsub(/$linefromoldwords/,$linefromnewwords);print}' input.txt >> output.txt
    number=$number+1
echo $number
done <  old.text

However, my solution does not work well. When I run the script:

  • On line 6, the sed command does not know where the $number ends.
  • The $number variable is changing to “0+1”, then “0+1+1”, when it should change to “1”, then “2”.
  • The line with awk does not appear to be doing anything more than copying the input.txt exactly as is to output.txt.

Do you have any suggestions?

Update:

The marked answer works well, however, I use this script a lot and it takes many hours to finish. So I offer a bounty for a solution which can complete these replacements much quicker. A solution in BASH, Perl, or Python 2 will be okay, provided it is still UTF-8 compatible. If you think some other solution using other software commonly available on Linux systems would be faster, then that might be fine too, so long as huge dependencies are not required.

  • 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-27T03:10:10+00:00Added an answer on May 27, 2026 at 3:10 am
    • One line 6, the sed command does not know where the $number ends.

    Try quoting the variable with double quotes

    linefromnewwords=$(sed -n “$number”p newwords.txt)

    • The $number variable is changing to “0+1”, then “0+1+1”, when it should change to “1”, then “2”.

    Do this instead:

    number=`expr $number + 1`

    • The line with awk does not appear to be doing anything more than copying the input.txt exactly as is to output.txt.

    awk won’t take variables outside its scope. User defined variables in awk needs to be either defined when they are used or predefined in the awk’s BEGIN statement. You can include shell variables by using -v option.

    Here is a solution in bash that would do what you need.

    Bash Solution:

    #!/bin/bash
    
    while read -r sub && read -r rep <&3; do
      sed -i "s/ "$sub" / "$rep" /g" main.file
    done <old.text 3<new.text
    

    This solution reads one line at a time from substitution file and replacement file and performs in-line sed substitution.

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

Sidebar

Related Questions

We have following problem. Developers frequently need to make small changes to our web
I have a few batch files I need to run frequently in developing a
I have a file called error.log on my server that I need to frequently
I need to merge between dev and master frequently. I also have a commit
I have a private LinkedList in a Java class & will frequently need to
I have a server that generates pngs very rapidly and I need to make
I frequently need to ssh into a server, but I can't ssh into it
In data processing, I frequently need to create a lookup data structure to map
I am using nLog in a large financial application, but frequently need to bring
As an optimization, I decided to put an object I frequently need - an

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.