How to replace a set of words in a file with another set in Perl?

My requirement is to replace the set of words in a given text file with a second set of words that can be specified from the command line or another file. Desire to use Perl for this, since the rest of my code is also in Perl.

So, if I have the following:

server name="${server1}" host="abc.com" server name="${server2}" host="webcs.com" server name="${server5}" host="httpvcs1.com" server name="${server6}" host="xyz.com" server name="${server7}" host="msg.com" 

I want to replace the lines 'server1', 'server2', 'server5', etc. with a different set of words. They can be placed in another file or specified on the command line (whichever is more possible).

In addition, if instead of "server1", "server2", etc. I want to replace the word "server" with the word "file", how would I start making a regular expression for this replacement?

 perl -pie 's/server\d{1-3}/myword/g' loginOut.txt > loginOut1.txt 

The above will replace all words with the word "myword". But I want to replace only the substring.

+3
source share
6 answers

You can try the Template Toolkit .


Here is an excerpt from the Template Toolkit Introduction, manual page :


Template Toolkit is a set of Perl modules that implement a fast, flexible, powerful, and extensible template processing system. It is most often used to create dynamic web content, although it can be used equally well for processing any text documents.

At the simplest level, it provides an easy way to process template files by filling in the built-in variable references with their equivalent values. Here is an example template.

  Dear [% name%],

 It has come to our attention that your account is in 
 arrears to the sum of [% debt%].

 Please settle your account before [% deadline%] or we 
 will be forced to revoke your license to Thrill.

 The management.

By default, template directives are embedded in the sequence of characters [% ... %] , but you can change them and various other parameters to customize how the Template Toolkit looks, feels and works. You can set the INTERPOLATE parameter, for example, if you prefer to embed your variables in the Perl style:

  Dear $ name,

 It has come to our attention that your account is in 
 arrears to the sum of $ debt.

Perldoc Page

0
source

The regular expression for your second question would be s/server/myword/g; . This matches (and replaces) any appearance of a "server".

To replace server1, server2, etc. with a different line, each one can have a text file containing a replacement rule, for example:

 server1 abcd server2 bcde server3 cdef etc. 

Then you read the date from the file to the hash, for example,

 my %dict; while(<DICTFILE>){ /(\S+)\s+(\S+)/; $dict{$1}={$2}; } 

and then do the replacement:

 while(my $line = <>){ foreach my $s (keys %dict){ $line =~ s/$s/$dict{$s}/g; } print $line; } 
+2
source

All answers above have some drawbacks. You asked some way

I want to replace 'server1', 'server2', 'server5', etc. with something like 'file1', 'file2', 'file5', ..

The command for this (on the Windows command line is -pe, not -pie):

 perl -pe "s/\{server/\{file/g" in.txt > out.txt 

and out.txt:

 server name="${file1}" host="abc.com" server name="${file2}" host="webcs.com" server name="${file5}" host="httpvcs1.com" server name="${file6}" host="xyz.com" server name="${file7}" host="msg.com" 

I believe that this is exactly what you wanted based on your last comment.

+1
source

You just need to take as much as you need to make it unique to your situation.

In this case, you can do:

c / {server / {file / g;

+1
source

Change your regex to the following:

 perl -pie 's/\{server/myword/g' loginOut.txt > loginOut1.txt 
0
source

Try the following:

 $what = 'server'; # The word to be replaced $with = 'file'; # Replacement s/(?<=\${)$what(?=[^}]*})/$with/g; 
0
source

All Articles