CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Replacing 'ABC' string with 'abc:@192.1681.1.'

 



varunparihar
New User

May 25, 2015, 4:38 AM

Post #1 of 7 (2980 views)
Replacing 'ABC' string with 'abc:@192.1681.1.' Can't Post

Hi,

I want to replace 'ABC' string with 'abc:@192.1681.1.' using perl.

Any help would be appreciated.

Thanks,
Varun Parihar


BillKSmith
Veteran

May 25, 2015, 5:36 AM

Post #2 of 7 (2978 views)
Re: [varunparihar] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

You probably need a regular expression in a substitute operator. I cannot offer any more help without more information.

Do you want to replace 'ABC' in a longer string? In file? In a database? In???

Can it occur more than once? Do you want to replace the first? The last? All?

Do you need help finding 'ABC' or only with the replacement?

Do you want to replace it permanently or do you want a new copy with the change?
Good Luck,
Bill


varunparihar
New User

May 25, 2015, 5:51 AM

Post #3 of 7 (2976 views)
Re: [BillKSmith] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

Thanks for your reply.

Scenario is:

File 1 contains:
Database_URL=Substitution_variable

File 2 contains:
Substitution_variable=jdbc:oracle:thin:@192.168.1.1:1521:tiborcl

I have a script which reads the value of Substitution_variable from File 2 and put it in File 1.

Expected File 1 content after replacement:
Database_URL=jdbc:oracle:thin:@192.168.1.1:1521:tiborcl

Actual File 1 content after replacement:
Database_URL=jdbc:oracle:thin:.168.1.1:1521:tiborcl

"@192" is missing from File 1 after replacement.

Please help.


BillKSmith
Veteran

May 25, 2015, 6:32 AM

Post #4 of 7 (2973 views)
Re: [varunparihar] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

I cannot point out your error without seeing your work. The following example works fine.

Code
use strict; 
use warnings;
my $file_1 = 'Database_URL=ABC' . "\n";
my $file_2 = 'ABC=jdbc:oracle:thin:@192.168.1.1:1521:tiborcli' . "\n";


open my $FILE_1, '<', \$file_1;
open my $FILE_2, '<', \$file_2;

my $f2 = <$FILE_2>;
my ($text, $substitution) = split /\=/, $f2;

my $f1 = <$FILE_1>;
$f1 =~ s/$text/$substitution/;

print $f1;


OUTPUT:

Code
Database_URL=jdbc:oracle:thin:@192.168.1.1:1521:tiborcli


Note: I used in-memory files rather than disk files so you can see their contents.
Good Luck,
Bill


varunparihar
New User

May 25, 2015, 11:11 PM

Post #5 of 7 (2952 views)
Re: [BillKSmith] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

Thanks for the code Bill. However, I would like to know if i can achieve using/modifying below command:

perl -p -i -e 's,$var1,$var2,g'

where,
var1 = ABC
var2 = jdbc:oracle:thin:@192.168.1.1:1521:tiborcl

Thanks!


BillKSmith
Veteran

May 26, 2015, 6:27 AM

Post #6 of 7 (2943 views)
Re: [varunparihar] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

The short answer is "no".

Your new command is command to your operating system to run perl with three options. The -p tells perl to loop through the standard input, process each record with the code in -e'...', and print the result. The -i specifies that the changes are to be made to the original file. You have not specified an input file. Perl will use your operating system default (almost certainly your keyboard). Your code inside the -e option is a perl substitution command using two scalar variables. You have told me what they should contain, but you have not told perl. They are both global variables with the value undef. The -p option will never pass in an undefined string, so the substitute command will never make a match. With no match, it does not do anything. If you try to run your command, it will appear to "hang" because it is waiting for input from your keyboard.

The -p and -i options are seldom a good idea when you need to read more than one file. By default, perl concatenates them, which is not what you want.

The code that you need to process one line would look like the code in my previous post, except that you would open disk files instead of memory files. (Refer perldoc -f open). If you wish to loop through the second file and/or save the results to another file, you must add the code to do that.
Good Luck,
Bill


aaron_baugher
Novice

Jun 2, 2015, 9:57 PM

Post #7 of 7 (2886 views)
Re: [varunparihar] Replacing 'ABC' string with 'abc:@192.1681.1.' [In reply to] Can't Post

The @192 part of your string is disappearing because perl thinks @ is the beginning of an array variable, so it interpolates it. Look into the quotemeta function for info on how to prevent that.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives