Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Re: Regular Expression Problem

by Chmrr (Vicar)
on Nov 26, 2002 at 20:19 UTC ( #215922=note: print w/ replies, xml ) Need Help??


in reply to Regular Expression Problem

To rephrase your problem in a way that may make the answer more obvious, you need to:

  1. Edit the file in-place
  2. Keep track of the most recent CREATE TABLE line
  3. Extract the table name out of it if it exists, and use it to keep up-to-date what table we're in
  4. Otherwise, replace every occurrance of the string "PRIMARY KEY" with the string "CONTRAINT PK_(table name) PRIMARY KEY"

Armed with this knowledge, we can go about writing something that does this. I don't know enough about your problem to do #3 above reliably, so you'll probably have to change that regex in the following code:

#!/usr/bin/perl -pi # The -p wraps the following code in a loop for each line, # and the -i tells perl we're editing the file in-place. # Give this program the filename(s) to change as arguments # on the command line. # Extract and update the table name if this is a CREATE TABLE line $table = $1 if (/CREATE TABLE "SO"."([^"]+)"/); # Change the "PRIMARY KEY" bit if we find it. s/(PRIMARY KEY)/CONTRAINT PK_$table $1/;

I hope that makes some amount of sense.

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'


Comment on Re: Regular Expression Problem
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://215922]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-04-23 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (553 votes), past polls