LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2009, 09:45 PM   #1
sol_nov
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
Match pattern and replace


Hi All,

I am new to unix shell scripting, I need your help guys in coming up with some thing for the following scenario:

I have an .sql file which contains:

####This resides in .sql file ----------------------------------------------

INSERT INTO /*<base>*/ABC_BASE.tablename

SELECT
col1, col2

FROM
/*<acc>*/ABC_ACC.tablename2

WHERE
so_dt >= /*<from_date>*/'01-01-2006'
AND
so_dt < /*<to_date>*/'01-15-2006'

####This is the desired output--------------------------------------

INSERT INTO /*<baseDB>*/${base}.${xyz}${def}_tablename

SELECT
col1, col2

FROM
${acc}.ABC_ACC.tablename2

WHERE
so_dt >= /*<from_date>*/'${from_dt}'
AND
so_dt < /*<to_date>*/'${to_dt}'

The tags are commented, by using tags we have to match the pattern and replace as above.

This is the opposite of the regualr stuff

Thanks for you time guys... waiting for ur answers
 
Old 09-01-2009, 04:30 AM   #2
noden
LQ Newbie
 
Registered: Aug 2009
Location: Denmark
Distribution: Debian
Posts: 29

Rep: Reputation: 18
You want to replace above with below or wice verca?
 
0 members found this post helpful.
Old 09-01-2009, 11:19 AM   #3
sol_nov
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
I want replace above with below
 
1 members found this post helpful.
Old 11-30-2009, 06:15 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Welcome to LQ.

Sorry that I can't help w/ your Q; sorrier that you got prematurely bumped from the 0-reply list & its 3 days of auto-bumping.

Did you find a solution on your own?
 
Old 11-30-2009, 06:32 AM   #5
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
It looks like you want to look into sed - it's quite an involved subject, but there are plenty of good tutorials on t' internet.
 
Old 11-30-2009, 07:48 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Frankly, for anything that fiddly, I'd use Perl. It's very good at that kind of text mangling.
 
Old 11-30-2009, 08:08 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I agree with chrism in that it's a "bit fiddly" because of the special characters, but since I'm not into perl and not great with AWK, I'd probably try to use sed; with some escapes of the special chars, it shouldn't be that hard.

PS - @ archtoad -- I just noticed the dates :/ wonder what happened..

Last edited by GrapefruiTgirl; 11-30-2009 at 08:10 PM. Reason: added PS
 
Old 11-30-2009, 08:23 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
FWIW, I made a text file named "weird" containing the string:

/*<acc>*/ABC_ACC.tablename2

Then ran a sed command on the file, to replace that string with the desired one: ${acc}.ABC_ACC.tablename2

like so:

shell$ sed -i 's%\/\*<acc>\*\/ABC_ACC.tablename2%${acc}.ABC_ACC.tablename2%' "weird"

and that worked fine.. So, using the /g suffix on the sed command would proceed through the whole file. And, using the same methodology, you can do the same sort of search+replace for the other selections you want to switch..

Sasha

Last edited by GrapefruiTgirl; 11-30-2009 at 08:24 PM.
 
  


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
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
pattern match : korn shell tostay2003 Programming 1 12-31-2008 12:08 PM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 12:50 PM
perl pattern match question lluciano Programming 4 02-28-2006 05:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 PM.

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