Wednesday, March 2, 2011

Re: search and replace with word list

On 03/02/2011 04:21 PM, raleighr3 wrote:
>> I understand what you want, file1.txt looks something like
>>
>> ...
>> <Folder><![CDATA[C:\work\sources\foo\bar\baz\MODULEX\lib]]></Folder>
>> ...
>> <Folder><![CDATA[C:\work\sources\foo\bar\baz\MODULEX\lib]]></Folder>
>> ...
>>
>> and you have file2.txt which looks something like
>>
>> ...
>> spatula
>> kumquat
>> ...
>>
>> and you'd like the output to look something like
>>
>> ...
>> <Folder><![CDATA[C:\work\sources\foo\bar\baz\spatula\lib]]></Folder>
>> ...
>> <Folder><![CDATA[C:\work\sources\foo\bar\baz\kumquat\lib]]></Folder>
>> ...
>>
>> such that the first instance of MODULEX gets replaced with the
>> first item in file2.txt, the second instance of MODULEX gets
>> replaced with the second item from file2.txt, etc until you run
>> out of matches in either file1.txt or file2.txt
>
> You've hit the nail on the head. This is a perfect understanding of
> what I'm after, sorry for the confusing explanation.

I think what I'd do is run a little magic over file2.txt to turn
it into vim commands that can then be executed:

" edit file2.txt
:e file2.txt

" convert the one-per-line replacements
" to commands that search for the next
" instance of "MODULEX" and replace it
" with the item from file2.txt
:%s@.*@:-/MODULEX/s//&

" yank the entire file/script
" into the scratch register
:%y

" split a window to open file1.txt to modify
:sp file1.txt

" execute the script as a macro
" (note this is normal-mode
" not an Ex command)
@"

Odd things may happen if
- you have entries in file2.txt that are replacement metachars
such as "\1", "&" or "/"

- your replacements include the value "MODULEX" which then may
get subsequently replaced.

Hope that helps you out,

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment