The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Determine string in Perl. ejdv Shell Programming and Scripting 4 05-14-2008 01:34 AM
find a string and get the next string sbandla Shell Programming and Scripting 3 04-17-2008 05:21 PM
search for a string -perl meghana Shell Programming and Scripting 11 02-12-2008 06:44 PM
string matching in perl ammu Shell Programming and Scripting 3 07-16-2007 11:47 AM
String Replacement with Perl Lindarella Shell Programming and Scripting 4 09-29-2006 11:05 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-24-2008
Registered User
 

Join Date: Nov 2006
Posts: 38
Find String in Perl

Hi all,

I am trying to search a SQL string and trying to find the table name in the query.

For e.g.

Select a,b,c,d,e,f from ITS.table where ITS.x=name

In the above string i need to identify the word ITS and replace with owner.
And also the name is not always ITS, it can be any word. So i need to identify the from keyword and the first '.' symbol and identify the word and then try to replace it.

I am trying to do it in PERL .

thanks in advance
Reply With Quote
Forum Sponsor
  #2  
Old 04-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Code:
s/\S+\./owner./g
\S matches non-spaces and + means as many as possible, and \. is a literal dot. So any dot with a sequence of non-space characters before it gets replaced with "owner.".
Reply With Quote
  #3  
Old 04-24-2008
Registered User
 

Join Date: Nov 2006
Posts: 38
Hi Era,

Thanks for the reply.

But first of all I need to identify the string inbetween the from and . and then need to replace the string identified with owner.

Because the string is not always static for different query the table names changes.

Thanks in advance
Reply With Quote
  #4  
Old 04-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
I don't understand. Are there places where the table should not be renamed to "owner"? Or are there tables which should not be replaced? Or are there other lines with dots where you don't want the substitution to happen? Then do it conditionally:

Code:
s/\S+\./owner./g if m/ from /;
... assuming the word "from" with spaces on both sides is a good way to identify the lines where the substitution needs to happen.

Or do you even have lines with more dots where not all of them concern the same table?

Code:
if (m/ from (\S+)\.) { s/$1\./owner./g }
This will take the word after the "from" and only replace that, on that line. (This is what you were asking all along, I know, but if the conditions are more relaxed then a simpler code is better, and you were not very clear on the precise conditions).
Reply With Quote
  #5  
Old 04-24-2008
Registered User
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 63
Ah, more than one way to do it... I too think that "from" is the key here because the XYZ.TABLENAME always follows the word "FROM".

SQLstr =~ /[FROM | from]\s(\w+)\./;

OK so... find FROM (in upper or lowere case) followed by one or more spaces \s followed by one or more WORD \w+ characters (i.e. letters) followed by a literal dot \.

Now the parentheses around \w+ actually captures that part of the match in $1, so next you can substitute that piece alone....

SQLstr =~ s/$1/OWNER/e

The "e" causes the $1 to be EVALUATED so that the resulting string is expanded before the substitution....
Reply With Quote
  #6  
Old 04-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 327
this will not work:
Code:
SQLstr =~ /[FROM | from]\s(\w+)\./;
[FROM | from] is a character class which matches any of the characters inside in no particular order. Should be:
Code:
$SQLstr =~ /(FROM|from)\s(\w+)\./;
or maybe:

Code:
$SQLstr =~ /from\s(\w+)\./i;
although using case insensitive matching might not be appropriate in this situation since SQL statements tend to be written in all upper or all lower case.

You also do not need the "e" option in the last regexp. $1 does not need to be evaluated, it needs to be interpolated and the left side of a substitution regexp is interpolated like a double-quoted string.
Reply With Quote
  #7  
Old 04-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
The other mistake is that the table name should be replaced elsewhere on that line, too (and maybe elsewhere, too; mahalakshmi was not very clear on the broader context of this).
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0