![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
s/\S+\./owner./g |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 /; 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 }
|
|
#5
|
|||
|
|||
|
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.... |
|
#6
|
|||
|
|||
|
this will not work:
Code:
SQLstr =~ /[FROM | from]\s(\w+)\./; Code:
$SQLstr =~ /(FROM|from)\s(\w+)\./; Code:
$SQLstr =~ /from\s(\w+)\./i; 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. |
|
#7
|
|||
|
|||
|
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).
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|