My friend Pierre found another way to solve the same problem using the Nedit text editor:

The following Nedit macro does the same thing you described.
I haven't tested it on a large file though (the macro below reads the entire file in memory...) This simple example takes a file1 with values to search for and replaces the contents of file2. Nedit has a nice C-like macro language (documented here:www.nedit.org/ftp/contrib/misc/nedit.pdf)

file1
value_to_search1 value_to_replace1
value_to_search1 value_to_replace1
etc

A macro can be added to Nedit by selecting:

Preferences->Default Menu->Customize Menu->Macro Menu

A dialog window pops up, select New, name your macro add it in the macro editor and click on Apply. After it will appear in the Macro menu and can be executed from there.

========== nedit edit macro ====================

t_print("Automatic Search & Replace","\n")
file1 = read_file("/home/pierre/tmp/nedit/file1")
file2= read_file("/home/pierre/tmp/nedit/file2")
file1_array = split(file1,"\n")

for(i=0;i<file1_array[]-1;i++) {
line_array = split(file1_arrayi," ")
file2= replace_in_string(file2,line_array[0],line_array1)
}

write_file(file2,"/home/pierre/tmp/nedit/file2_changed")

=================================================

I should note that Pierre's "Nedit" technique doesn't solve the
foo bar
ba BAH
problem, but this may be a good enough solution for many cases.


- Chris Koknat

In reply to Re^4: Algorithm to search and replace on data file, based on a table file? by koknat
in thread Algorithm to search and replace on data file, based on a table file? by koknat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.