Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-04-11 18:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found