Matching single quote in a regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching single quote in a regular expression
# 1  
Old 05-20-2011
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.

Code:
$ENV{'ORACLE_HOME'}

I tried this regluar expession:

Code:
/\$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?

Last edited by radoulov; 05-21-2011 at 08:38 AM.. Reason: Code tags.
# 2  
Old 05-20-2011
Hi,

It would be helpful to see some of the code but try:
Code:
/\Q$ENV{'ORACLE_HOME'}\E/

Regards,
Birei
# 3  
Old 05-20-2011
Tried /\Q$ENV{'ORACLE_HOME'}\E/, no prompt, but no match either. It took out the single quotes and gave me $ENV{ORACLE_HOME}

Last edited by radoulov; 05-21-2011 at 08:38 AM.. Reason: Code tags.
# 4  
Old 05-21-2011
How about this,

Code:
#!/usr/bin/perl

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

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

# 5  
Old 05-21-2011
I am sorry, I should have put in the whole code. Here the code:
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.

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 05-21-2011 at 03:14 PM.. Reason: Code tags
# 6  
Old 05-21-2011
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 ,


Quote:
Originally Posted by JC9672
I am sorry, I should have put in the whole code. Here the code:
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.

Moderator's Comments:
Mod Comment Please use code tags
# 7  
Old 05-22-2011
I have over 200 perl scripts in a directory. They have the following line in them:
Code:
$ENV{'ORACLE_HOME'}      = "/home/oracle/product/10.1.0/db_1"

I want to replace the line with the following:
Code:
$ENV{'ORACLE_HOME'}      = "/opt/app/d1gsa1d1/oracle/product/d1gsa1d1";

I want to replace the line the simple possible way. I used the following command:
Code:
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).

Last edited by Franklin52; 05-24-2011 at 12:37 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

2. Shell Programming and Scripting

regular expression matching whole words

Hi Consider the file this is a good line when running grep '\b(good|great|excellent)\b' file5 I expect it to match the line but it doesn't... what am i doing wrong?? (ultimately this regex will be in a awk script- just using grep to test it) Thanks, Storms (5 Replies)
Discussion started by: Storms
5 Replies

3. UNIX for Dummies Questions & Answers

delete lines matching a regular expression

I have a very large file (over 700 million lines) that has some lines that I need to delete. An example of 5 lines of the file: HS4_80:8:2303:19153:193032 153 k80:138891 HS4_80:8:2105:5544:43174 89 k88:81949 165 k88:81949 323 0 * = 323 0 ... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Regular expression matching

Hi, I have a variable in my script that gets its value from a procstack output. It could be a number of any length, or it could just be a '1' with 0 or more white spaces around it. I would like to detect when this variable is just a 1 and not a 1234, for example. This is as far as I got: ... (3 Replies)
Discussion started by: tmf33uk
3 Replies

6. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

7. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

8. Shell Programming and Scripting

Help: Regular Expression for Negate Matching String

Hi guys, as per subject I am having problem with regular expressions. Example, if i got a string "javax.servlet.http.HttpServlet.service" that may occurred anywhere within a text file. How can I used the negate pattern matching of regular expression? I tried the below pattern but it... (4 Replies)
Discussion started by: DrivesMeCrazy
4 Replies

9. Programming

Regular Expression matching in PERL

I am trying to read a file and capture particular lines into different strings: LENGTH: Some Content here TEXT: Some Content Here COMMENT: Some Content Here I want to be able to get (LENGTH: .... ) into one array and so on... I'm trying to use PERL in slurp mode but for some reason... (8 Replies)
Discussion started by: Legend986
8 Replies

10. Shell Programming and Scripting

Regular expression matching a new line

I have written a script to test some isdn links in my network and I am trying to format the output to be more readable. Each line of the output has a different number of digits as follows... Sitename , spid1 12345678901234 1234567890 1234567 , spid2 1234567890 1234567890 1234567 Sitename , ... (1 Reply)
Discussion started by: drheams
1 Replies
Login or Register to Ask a Question