Find String in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find String in Perl
# 1  
Old 04-24-2008
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
# 2  
Old 04-24-2008
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.".
# 3  
Old 04-24-2008
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  
Old 04-24-2008
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).
# 5  
Old 04-24-2008
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  
Old 04-25-2008
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.
# 7  
Old 04-25-2008
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

8. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

9. Shell Programming and Scripting

[Perl] Find one string, change another string.

Hi, In principle I am searching for a Perl equivalent for this sed command: sed "/TIM_AM_ARGS=/ s/60/1440/" $EDIT_FILE > $TEMP_FILE cp $TEMP_FILE $EDIT_FILE I was wondering if it needs to be like this, or that there other, shorter, alternatives: open (TIMENVFILE, "<$timenvfile") or die... (5 Replies)
Discussion started by: ejdv
5 Replies

10. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies
Login or Register to Ask a Question