Incremental replace functionality

cirrrocco

Golden Member
Sep 7, 2004
1,952
78
91
repost from Here.

Hey there, thanks for the response. I did not want to change an unique identifier in a bunch of files, but rather change the unique identifier in ONE file with the contents of a second file. so if i have ten instances of a unique identier say "****HHH***" in the first file, then lets suppose i have 10 different words each on a separate line. the first word from file 2 should replace the first instance of ****HHH*** in file 1 and the word on the second line should replace the second instance of ****HHH*** in file 1 and so forth.

ex:
COntents of file 1

ABCD = ABCD + ****HHH***
href=fox****HHH***.html
why does ****HHH*** fly high??

contents of second file

EFG
sucks
the bird

========
so after operation the content of file 1 should be

ABCD = ABCD + EFG
href=foxsucks.html
why does the bird fly high??


I hope that made sense.

======

so can someone find me a software that can do that, preferably one that a dumb user like me can run from my pc.

 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,563
4,478
75
Given file1.txt, file2.txt, the unique identifier above, and Strawberry Perl, the following at the command line should do it:

perl -e "$uniqueid='\*\*\*\*HHH\*\*\*';open(IN, 'file2.txt');while(<>){while(/$uniqueid/){$rep=<IN>;$rep=~s/[\r\n]//g;s/$uniqueid/$rep/;}print;}close IN;" file1.txt > result.txt

It even works with multiple instances of the unique identifier on the same line.

Edit: Fixed, Thx Mark
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Ken g6
Given file1.txt, file2.txt, the unique identifier above, and Strawberry Perl, the following at the command line should do it:

perl -e "$uniqueid='\*\*\*\*HHH\*\*\*';open(IN, 'file2.txt');while(<>) {while(/$uniqueid/){$rep=<IN>;$rep=~s/[\r\n]//g;s/$uniqueid/$rep/;}print;}close IN;" file1.txt > result.txt

It even works with multiple instances of the unique identifier on the same line.

Edit: Weird forum problem; replace that wink with a single end-parenthesis.

When you post something that contains combinations that might be interpreted as emoticons, check the "Do not parse emoticons" checkbox at the bottom of the edit dialog. That will keep your text from being converted.
 

cirrrocco

Golden Member
Sep 7, 2004
1,952
78
91
wow guys , let me check that out. That looks sweet and that strawberry looks so delicious..
 

cirrrocco

Golden Member
Sep 7, 2004
1,952
78
91
and also i am on a mac now, but will check it on xp once I get home. one question I had though was that is result.txt the output or will i have to check file1.txt after the replace is done. sorry I dont know any programming other than a lil bit html. Thanks Ken, Mark.