l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2001 Dec 30 17:10

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Looking for 2 cool regexs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Looking for 2 cool regexs

On Wed, Sep 12, 2001 at 04:25:47PM -0500, Jay Strauss wrote:
> I have a flag in my pgm that indicates to look for "create table" statements
> OR anything else.  The end result I'd like is that my scalar ($rebuilt) will
> equal all the create table statements, or everything else.

> a couple of regexs, one to null out anything not a "create table" statement,
> one to null out all the "create table" statements.
> But alas, I don't know how.
> I run my pgm by:
> # cat sample.data | tmp
> Jay
> #!/usr/bin/perl
> undef $/;
> my $file = <>;
> $file =~ s/\bREM\s+//g;
> $file =~ s/\n//g;
> my $flag = "t";
> my $rebuilt = '';
> foreach my $line (split(/;/,$file)) {
>    $create = ($line =~ /^CREATE\s+TABLE/) ? 1 : 0;
>    if ($flag eq "t") {
>       $line = '' if ! $create;
>    }
>    else {
>       $line = '' if $create;
>    }
>    $rebuilt .= $line.";" if ($line);
> }

this loop can be :

  $file =~ s/(;?\s*)(CREATE\s+TABLE[^;]+)(.*)/$1$3/g unless $t;
  $file =~ s/(;?\s*)(CREATE\s+TABLE[^;]+)(.*)/$2/g if $t;

which should simplify to (and should run faster)

  $file =~ s/\b(CREATE\s+TABLE[^;]+)//g unless $t;
  $file = join('', $file =~ m/\b(CREATE\s+TABLE[^;]+)/g) if $t;

though I don't use \b often enough to be sure of its operation in every

(should be close... (;?\s*) is a weak attempt to avoid CREATE TABLE inside
strings... (you used \b instead)...   if you did a:
  $file=join(";\n", split(/;/, $file)) . ";\n";  
you could use s///m for multiline, and then use ^ and $ for "start of line" 
from a \n perspective.)

> foreach my $line (split(/;/,$rebuilt)) {
>   print substr($line,0,80)."\n";
> }

this hard coded 0,80 is suspect, but if your input data is assured it's
not a problem (which doesn't appear to be the case).  You'll also lose
your ; characters.

Ted Deppner

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.