Matching single quote in a regular expression

I trying to match the begining of the following line in a perl script with a regular expression.

$ENV{'ORACLE_HOME'}

I tried this regluar expession:

/\$ENV\{\'ORACLE_HOME\'\}/

Instead of match, I got a blank prompt >

It seems to be a problem with the single quote. If I take it out it runs, it does not match, but runs.

Can someone help me out with right regluar expression?

Hi,

It would be helpful to see some of the code but try:

/\Q$ENV{'ORACLE_HOME'}\E/

Regards,
Birei

Tried /\Q$ENV{'ORACLE_HOME'}\E/ , no prompt, but no match either. It took out the single quotes and gave me $ENV{ORACLE_HOME}

How about this,

#!/usr/bin/perl

while(<DATA>) {
print if (/\$ENV{\'ORACLE_HOME\'}/);
}

__DATA__
$ENV{'ORACLE_HOME'}
$ENV{'ORACLE_HOME'
$ENV{'d;''ORACLE_HOME'}ldkld
ldkdlldl

I am sorry, I should have put in the whole code. Here the code:

perl -pi -w -e 's/\$ENV{\'ORACLE_HOME\'}\$ENV1\{\'ORACLE_HOME\'}/g;' *.pl

I get the prompt and it does not replace the text

This is just a test, if I can get it to work then I will add more that I have tried and it did work.

what exactly you want to do ? I mean what is the scenario and expected output ?

Here you are trying something and its not working, there could be better way of doing same if we know what you want.

Going back to your question, $ENV is perl hash to get environments variables. I am not sure what is $ENV1 ? or you are just doing it for testing?

you missed 2nd separator below ,

I have over 200 perl scripts in a directory. They have the following line in them:

$ENV{'ORACLE_HOME'}      = "/home/oracle/product/10.1.0/db_1"

I want to replace the line with the following:

$ENV{'ORACLE_HOME'}      = "/opt/app/d1gsa1d1/oracle/product/d1gsa1d1";

I want to replace the line the simple possible way. I used the following command:

perl -pi -w -e 's/\$ENV\{\'ORACLE_HOME\'\}      \= \"\/home\/oracle\/product\/10.1.0\/db_1\"\;/\$ENV\{\'ORACLE_HOME\'\}      \= \"\/opt\/app\/d1gsa1d1\/oracle\/product\/d1gsa1d1\"\;/g;' *.pl

(I bolded and underline the command so you can see what I am doing)

I get no match, no replacement, just a prompt >

I tested replacing the line from the = sign to the end of the line with the above command and it works, but I can not use that because I have 5 lines to replace in script with each having excactly the same 2nd half and a different 2nd for each of the 5 lines. The first part of each line starts with $ENV{', and single quote is part that does not work. If I had lines that did not have the single quote in the line, the above command would work (without the single quote of course).


# Lets print the file contents 

C:\Users\Ninz\Desktop>type a.txt
This is a fake file

$ENV{'ORACLE_HOME'} = "/home/oracle/product/10.1.0/db_1" ;

this is the last line

# using windows , using different separator "|" instead of "/" so that you dont have to escape every "/" in path 

# Adding "-i.bak" because in windows it throws error if you try to modify file without taking backup 

C:\Users\Ninz\Desktop>perl -pi.bak -e "s|\$ENV{'ORACLE_HOME'} \= \"/home/oracle/
product/10.1.0/db_1\"| \$ENV{'ORACLE_HOME'} \= \"/opt/app/any/other/path\"|" a.txt

C:\Users\Ninz\Desktop>type a.txt
This is a fake file

 $ENV{'ORACLE_HOME'} = "/opt/app/any/other/path" ;

this is the last line
C:\Users\Ninz\Desktop>

The 200 perl scripts are on a Unix/Linux server, so I can't use a '|' and I have to escape '/' in the path. But that is not the problem. I can escape '/' in the path and and escape the '$' and escape the '{' and it will match and replace the text. The problem is the single quote before and after ORACLE_HOME. Does anyone know how to match and replace a single quote in a line of text in a file?

@JC9672 : Why cant you use different separator ?? Its valid and its not Platform specific.

I have copied below example on RH4 box and tried again, it worked without any modification ( If something works on windows then it should work on *nix without any problem :slight_smile: )

  1. Checking file contents, there are quotes before ORACLE_HOME
$ > cat a.pl
This is a fake file
$ENV{'ORACLE_HOME'} = "/home/oracle/product/10.1.0/db_1" ;
this is the last line
$ >
  1. Running replace command

$ > perl -pi -e "s|\$ENV{'ORACLE_HOME'} \= \"/home/oracle/product/10.1.0/db_1\"| \$ENV{'ORACLE_HOME'} \= \"/opt/app/any/other/path\"|" a.pl

  1. Checking the contents of file.

$ > cat a.pl
This is a fake file
$ENV{'ORACLE_HOME'} = "/opt/app/any/other/path" ;
this is the last line
$ >

  1. Both the entries one below other
$ENV{'ORACLE_HOME'} = "/home/oracle/product/10.1.0/db_1" ;
$ENV{'ORACLE_HOME'} = "/opt/app/any/other/path" ;

I copied and pasted your command and it did not work. I even copied your script and it did not work. I did not get any error message, but no replacement either. I am using a Red Hat box verison 8 or 9. I not sure which. Also my Perl version is 5.12.3. Maybe that is why it did not work.

dont copy paste, sometimes it gets converted to other characters

try on simple thing , create 1 file with $ENV{"ABCD"} try to change this value do it on windows then on unix

paste what you have done, you have to try different things because its a regular expression and all versions of perl + red hat will support that ..

best of luck :slight_smile: