I have a big text file with SQL query in between blocks of EXEC SQL --- END-EXEC.
I need everything in-between EXEC SQL --- END-EXEC. keywords. Sample Input is below.
This is how I'm trying to do. I'm able to get the contents if there is one block of EXEC SQL --- END-EXEC. However, If there are multiple EXEC SQL --- END-EXEC. blocks, I am failing.
a) Read the file as a string.
For reading whole file as string, I'm using
$content = [io.file]::ReadAllText("$pathtofile")
$content = $content -replace "\s*`n", " "
(p.s. I'm using V2.0 so cannot use -raw option)
b) Then I doing this to match everything in between EXEC keyword.
$result = $content -match "EXEC(.*)EXEC"
$matches[0] > D:\result.txt
Input :
* ABCD ABCD ABCD BLAH BLAH BLAH - This is some text preceding EXEC SQL
**EXEC SQL**
DECLARE TMPMOTT-CUR CURSOR FOR
SELECT KONTYP
,BFDAT
,MARK
,MOTT
,AVS
,BEL
,OKLBE
FROM ABCBEFGH
ORDER BY MOTT
**END-EXEC**.
* ABCD ABCD ABCD BLAH BLAH BLAH - This is some text after END-EXEC.
You need to use a lazy quantifier to make sure that your regex matches each
EXEC
block individually. And you can gather all matches with a single regex operation:Explanation:
Or:
With multiline mode