匹配Powershell中两个单词之间的所有内容

编程入门 行业动态 更新时间:2024-10-24 19:20:48
本文介绍了匹配Powershell中两个单词之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在EXEC SQL的两个段之间有一个带有SQL查询的大文本文件--- END-EXEC.

I have a big text file with SQL query in between blocks of EXEC SQL --- END-EXEC.

我需要介于EXEC SQL --- END-EXEC之间的所有内容.关键字.示例输入如下.

I need everything in-between EXEC SQL --- END-EXEC. keywords. Sample Input is below.

这就是我要尝试的方法.如果有一块EXEC SQL --- END-EXEC,我就能得到内容.但是,如果有多个EXEC SQL --- END-EXEC.块,我失败了.

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)以字符串形式读取文件.

a) Read the file as a string.

我要使用

$content = [io.file]::ReadAllText("$pathtofile") $content = $content -replace "\s*`n", " " (p.s. I'm using V2.0 so cannot use -raw option)

b)然后我执行此操作以匹配EXEC关键字之间的所有内容.

b) Then I doing this to match everything in between EXEC keyword.

$result = $content -match "EXEC(.*)EXEC" $matches[0] > D:\result.txt

输入:

* 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.

推荐答案

$script = Get-Content D:\temp\script.sql $in = $false $script | %{ if ($_.Contains("EXEC SQL")) { $in = $true } elseif ($_.Contains("END-EXEC")) { $in = $false; } elseif ($in) { Write-Host $_ } # Or Out-File ... }

更多推荐

匹配Powershell中两个单词之间的所有内容

本文发布于:2023-10-24 14:49:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所有内容   单词   两个   Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!