Frankly, I would do this interactively in Vim in less time than it would take to type any of the scripted solutions given so far.
Then, read in list1 at the top. (I'm assuming it will be easy to tell visually where it ends and where list2 starts, so no need to mark anything ahead of time—but you can
:1k a first if you like so the original line 1 of list2 is marked.)
After this command your cursor will be at the start of the file, on the first word
* to go to next instance of word under cursor. (That will be the line you want to comment.)
I to enter insert mode (at the first non-whitespace character on the line, since you used a capital
I and not lower case) and type
// and then press Escape.
n to go to next instance of searched word. (Since you're at the start of the line just after your
// characters, your cursor will go to the instance you just found. So press it again to go to the next instance.)
Assuming there was only the one instance to be commented, you'll now be at the start of the file again—the first line. Press
j to go down (or just press Enter to go to the first non-whitespace character of the next line—same result in this case).
* to go to the next
test3 instance, since that's what's under your cursor now.
. to repeat the "commenting out line" action. The dot command is awesome. :)
n again. (Twice.) In your example text, you're done now—you're back at the second line,
test3. If there were more lines to be commented, again just type
j*.nn. And if there is another, type
When done (you're on
test3 or whatever the last line was from
list1, just above the original first line from
dgg to delete all lines from the current line to the first line of the file—so that the
list1 entries won't be there anymore.
In total, what you type is
to open the file, then:
(Repeat however many times)
Then to save and exit, type
:x and press Enter.
I learned your "list1" is huge. Doesn't matter; just use a macro.
After doing the above a few times so you are sure it works, type:
This will record
j*.nn as a macro in register
Run the macro by typing
Run it again by typing
Then run it 4000 times by typing
4000@@. But personally, I would do it in smaller chunks. And perhaps not use the dot command in the macro but explicitly type
Point being, I would still do it interactively, and it would still take only a minute or two, regardless of how many lines I had to handle. The magic of Vim. :)