如何解析sqlplus查询中的详细信息?

时间:2016-01-30 15:16:16

标签: regex powershell

我有一个PowerShell脚本,使用sqlplus命令连接到Oracle数据库并执行查询。

结果如下:

SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 27 13:46:07 2016

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options


NAME
--------------------------------------------------------------------------------
Agent-Job Follow Up Missed Group D
Agent-Job Follow Up Missed Group E
Audit Archive
Day BGO - 705 and 705NA - A (2)
Day BGO - ERR Quebec - A
Dispatch Lunch Break Agent (Pre-Agent) - Group D
Helper Job Agent (Amend, Prevent, Lead) Group A
Incomplete Tasks Display Status Cleanup - Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group B
Lunch Break Report Agent(Post-Agent) - Group A

NAME
--------------------------------------------------------------------------------
MST Validation - Group A
MST Validation - Group C
Unschedule INHS Tasks
Unschedule Jobs with Schedule Update Failure - Group E

15 rows selected.

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

我需要解析这个结果并采取这一部分:

NAME
--------------------------------------------------------------------------------
Agent-Job Follow Up Missed Group D
Agent-Job Follow Up Missed Group E
Audit Archive
Day BGO - 705 and 705NA - A (2)
Day BGO - ERR Quebec - A
Dispatch Lunch Break Agent (Pre-Agent) - Group D
Helper Job Agent (Amend, Prevent, Lead) Group A
Incomplete Tasks Display Status Cleanup - Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group B
Lunch Break Report Agent(Post-Agent) - Group A

NAME
--------------------------------------------------------------------------------
MST Validation - Group A
MST Validation - Group C
Unschedule INHS Tasks
Unschedule Jobs with Schedule Update Failure - Group E

----------------------------------------------- - - - - - - -编辑 - - - - - - - - - - - - - - - - - - ------------------------

到目前为止我的尝试:

#Using a .txt file report to test
$test = Get-Content -path "C:\Scripts\PS SQL Queries\Agent Failure\Agent Failure Report27012016.0146.txt" | out-string
$start = $test | select-string -pattern "NAME"

这从“NAME”开始给我一个结果, 我现在正在寻找一个命令来获得我的选择字符串的反转。 具体来说,获取String直到“选择15行”。

3 个答案:

答案 0 :(得分:2)

你需要一个正则表达式" multiline" og"单线"修饰符,因此正则表达式将在换行符中存活并让你使用" start-anchor"确定在哪里停下来。试试这个:

#Using a .txt file report to test
$test = Get-Content -Path "C:\Scripts\PS SQL Queries\Agent Failure\Agent Failure Report27012016.0146.txt" | Out-String
$start = if($test -match '(?ms)(^NAME.*?)^\s+^\d') { $Matches[1].Trim() }

https://regex101.com/r/mN6gW9/1

答案 1 :(得分:1)

与大多数正则表达式类似Frode F.'s answer。仍然使用带有-AllMatches的Select-String,因为我们返回了多个匹配项。保留问题中的前几行我会针对$test运行此行。

看起来你想要"姓名"直到下一组双新线。这就是回归。

($test | Select-String -Pattern "(?s)Name.*?\r\n\r\n" -AllMatches).Matches.Value

根据您的需要,您可以通过一些后期处理获得更具体的信息。请记住,此查询返回了2个多行字符串。

答案 2 :(得分:-1)

一个肮脏(但简单)的解决方案是使用tee-object将信息输出到屏幕和文件,然后导入文件并解析。

我从未尝试过从powershell通过sqlplus运行oracle命令,我只是直接使用oracle.dataccess.dll和查询/更新。您是否尝试将整个命令设置为变量?

就像$ var =你正在运行的命令一样

查看该数据是否会填充变量,然后您可以非常轻松地解析它。不确定你是否尝试过这个。