user3056328
user3056328

Reputation: 1

How to get each function pattern in a file?

I have a file contains a lot of functions, each function like

=======

CREATE PROCEDURE [fName]

code...

code...

END

GO

=======

I want to get every function pattern, and the expression I used is

^(CREATE PROCEDURE).*(\r\n.*)+\r\nGO

The result is wrong. Can somebady help! Thanks for answer

Upvotes: 0

Views: 45

Answers (3)

roo2
roo2

Reputation: 6071

instead of using "." and \r\n try using [\s\S] it matches anything that is a whitespace OR isn't a whitespace including newlines.

CREATE PROCEDURE[\s\S]*GO

This may need some regex flags to be set

Upvotes: 0

richardtallent
richardtallent

Reputation: 35404

Try something more like:

(?<=^|\n)(CREATE PROCEDURE [\s\S]+?)(?=\r\nGO|$)

This will match every instance of CREATE PROCEDURE that starts a new line and the text that follows it, up to but not including the next GO directive, or the end of the file, whichever comes first.

In [\s\S]+?, the question mark makes this a non-greedy match, which is how we stop at the next GO instead of the last GO. [\s\S] is equivalent to [.\n], i.e., it matches any character.

I've assumed .NET-style regular expressions, since you didn't specify.

Upvotes: 0

perreal
perreal

Reputation: 98088

Using sed:

sed -n '/CREATE PROCEDURE/,/^END$/{H};/^GO$/{s/.*//;x;p;}' input

Upvotes: 1

Related Questions