LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2009, 05:22 AM   #1
olegk25
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Rep: Reputation: 0
regex or other solution to alter string fragment


please help!

I'm new to Linux and I'm looking for a regex (sed) or other simple way to format the SQL like

Code:
select ABREV from LIMITS, ABREV where ABREV='US'
into

Code:
select ABREV from SCHEMA.LIMITS, SCHMEA.ABREV where ABREV='US'
I've tried

Code:
sed 's/\(select.*from[ ]*\)\([A-Za-z0-9_]\)\(.*\)/\1SCHEMA.\2\3/'
but it produces only

Code:
select ABREV from SCHEMA.LIMITS, ABREV where ABREV='US'
any help please?

P.S. There may be any number of table names
 
Old 06-04-2009, 05:38 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Try this awk code:
Code:
/select/{
 for ( i=1; i<=NF; i++ ) {
   if ( $i == "where" )
      break
   if ( to_sub )
      sub(/.*/,"SCHEMA."$i,$i)
   if ( $i == "from" )
      to_sub = 1
 }
}1
it checks for "from" and "where" words in the select statement and alter the embedded words accordingly. Awk cannot modify the input file on the fly, so you have to redirect the output to a new file:
Code:
awk -f test.awk infile > oufile
where test.awk is the awk script above.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
RegEx: Is there a way to say "string NOT found" ? jdruin Programming 10 12-04-2008 12:41 PM
A simpler solution to my original post on regEx linuxmaveric Programming 6 04-04-2008 01:57 AM
Java read binary file in as string for RegEx mulciber Programming 1 12-18-2005 12:36 PM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration