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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:02:50+00:00 2026-05-26T15:02:50+00:00

I want to perform about many find and replace operations on some text. I

  • 0

I want to perform about many find and replace operations on some text. I have a UTF-8 CSV file containing what to find (in the first column) and what to replace it with (in the second column), arranged from longest to shortest.

E.g.:

orange,fruit2
carrot,vegetable1
apple,fruit3
pear,fruit4
ink,item1
table,item2

Original file:

"I like to eat apples and carrots"

Resulting output file:

"I like to eat fruit3s and vegetable1s."

However, I want to ensure that if one part of text has already been replaced, that it doesn’t mess with text that was already replaced. In other words, I don’t want it to appear like this (it matched “table” from within vegetable1):

"I like to eat fruit3s and vegeitem21s."

Currently, I am using this method which is quite slow, because I have to do the whole find and replace twice:

(1) Convert the CSV to three files, e.g.:

a.csv     b.csv   c.csv
orange    0001    fruit2
carrot    0002    vegetable1
apple     0003    fruit3
pear      0004    fruit4
ink       0005    item1
table     0006    item 2

(2) Then, replace all items from a.csv in file.txt with the matching column in b.csv, using ZZZ around the words to make sure there is no mistake later in matching the numbers:

a=1
b=`wc -l < ./a.csv`
while [ $a -le $b ]
do
    for i in `sed -n "$a"p ./b.csv`; do
        for j in `sed -n "$a"p ./a.csv`; do
            sed -i "s/$i/ZZZ$j\ZZZ/g" ./file.txt
            echo "Instances of '"$i"' replaced with '"ZZZ$j\ZZZ"' ("$a"/"$b")."
            a=`expr $a + 1`
            done
    done
done

(3) Then running this same script again, but to replace ZZZ0001ZZZ with fruit2 from c.csv.

Running the first replacement takes about 2 hours, but as I must run this code twice to avoid editing the already replaced items, it takes twice as long. Is there a more efficient way to run a find and replace that does not perform replacements on text already replaced?

  • 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-26T15:02:51+00:00Added an answer on May 26, 2026 at 3:02 pm

    One way to do it would be to do a two-phase replace:

    phase 1:
    
    s/orange/@@1##/
    s/carrot/@@2##/
    ...
    
    phase 2:
    s/@@1##/fruit2/
    s/@@2##/vegetable1/
    ...
    

    The @@1## markers should be chosen so that they don’t appear in the original text or the replacements of course.

    Here’s a proof-of-concept implementation in perl:

    #!/usr/bin/perl -w
    #
    
    my $repls = $ARGV[0];
    die ("first parameter must be the replacement list file") unless defined ($repls);
    my $tmpFmt = "@@@%d###";
    
    open(my $replsFile, "<", $repls) || die("$!: $repls");
    shift;
    
    my @replsList;
    
    my $i = 0;
    while (<$replsFile>) {
        chomp;
        my ($from, $to) = /\"([^\"]*)\",\"([^\"]*)\"/;
        if (defined($from) && defined($to)) {
            push(@replsList, [$from, sprintf($tmpFmt, ++$i), $to]);
        }
    }
    
    while (<>) {
        foreach my $r (@replsList) {
            s/$r->[0]/$r->[1]/g;
        }
        foreach my $r (@replsList) {
            s/$r->[1]/$r->[2]/g;
        }
        print;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table on which I want to perform some operations every hour.
I have an ARFF file containing 14 numerical columns. I want to perform a
What do I want to achieve: I want to perform some time consuming operations
I have an INPUT element of the TEXT type and I want to perform
I want to perform cascade delete for some tables in my database, but I'm
I want to perform some empirical trade-off's to assess the performance of applications written
It seems that every time I want to perform a db query, I have
I have many Action objects with a property long Timestamp . I want to
I want to perform rubyf action in VIM when I press F5 if the
I want to perform simple validation against multiple fields. Please note these fields are

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.