首页 > 解决方案 > Powershell - 从 Word Doc 中提取部分

问题描述

使用Powershell,我们成功提取了基于pattern的线条

get-content "C:\Search.doc" | 
select-string -pattern "means the aggregate of the Facility" a

但是我们如何从 word doc 中提取一个 SECTION。请参见下面的示例(我们需要提取点 (a) 和 (b) 位于 PURPOSE 部分下:

3.  PURPOSE
3.1 Purpose
(a) Each Borrower shall apply all amounts borrowed by it under Facility A:
(i) payment to the Vendor of the purchase price for the [xxxx] under the Acquisition Agreement; 
(b) [Each Borrower shall apply all amounts borrowed by it under the Revolving Facility towards the general corporate 

标签: powershell

解决方案


您可以使用该属性-Context

在匹配的行之前和之后捕获指定数量的行。这允许您在上下文中查看匹配。

例子:

$F = Select-String -Path "audit.log" -Pattern "logon failed" -Context 2, 3

第一个命令在 Audit.Log 文件中搜索短语“logon failed”。它使用 Context 参数捕获匹配前的 2 行和匹配后的 3 行。

在你的情况下:

3.  PURPOSE
3.1 Purpose
(a) Each Borrower shall apply all amounts borrowed by it under Facility A:
(i) payment to the Vendor of the purchase price for the [xxxx] under the Acquisition Agreement; 
(b) [Each Borrower shall apply all amounts borrowed by it under the Revolving Facility towards the general corporate 

Select-String -Pattern "under Facility A" -Context 0,2

推荐阅读