根据模式进一步过滤结果

时间:2018-07-15 14:45:12

标签: powershell

使用PowerShell,我们能够从文档中成功提取所需的行。

代码:

Get-Content "C:\Contract.doc" | 
    Select-String -Pattern "in relation to any Facility" | 
    Select -Property @{Name = 'Name'; Expression = {$_.Line}}

输出:

Name
----
in relation to any Facility A Loan [2% ] per cent. per annum;
in relation to any Facility B Loan [ 5% ] per cent. per annum;

我们正在寻找的是从上述输出中提取2%... 5%

代码,我们正在尝试对我们不起作用:

Get-Content "C:\Contract.doc" | 
    Select-String -Pattern "in relation to any Facility" | 
    Select -Property @{Name = 'Name'; Expression = {$_.Line}} |
    Select-String '\[\?+([^?]+)\?+\]' |
    ForEach-Object { $_.Matches.Groups[1].Value }

任何人都可以帮助如下提取方法:

"in relation to any Facility A Loan [2% ] per cent. per annum", "2%"

Word文档的一部分:合同文档

"Margin" means:
(a) in relation to any Facility A Loan [2% ] per cent. per annum;
(b) in relation to any Facility B Loan [ 5% ] per cent. per annum;
(c) [in relation to any Incremental Facility Loan, the percentage rate per annum specified as such in the Incremental Facility Notice relating to the Incremental Facility under which that Incremental Facility Loan is made or is to be made;]

3 个答案:

答案 0 :(得分:3)

检查下一个代码段。

Get-Content C:\Contract.doc |
Select-String -Pattern @'
\b(in relation to any Facility [A-Z] Loan \[\s*(\d+%)\s*\] per cent. per annum);
'@ |
Select-Object @{Name = 'Line'; Expression = {$_.Matches.Groups[1].Value}},
              @{Name = 'Result'; Expression = {$_.Matches.Groups[2].Value}}

要获取正则表达式\b(in relation to any Facility [A-Z] Loan \[\s*(\d+%)\s*\] per cent. per annum);的说明,请单击here

答案 1 :(得分:1)

使用Regular Expression,解决方案更加有效:

Get-Content .\contract.doc|
  Where-Object {$_ -match 'in relation to any Facility.*\[([\d% ]+)\]'}| 
    ForEach-Object{
      [PSCustomObject]@{
        Name  = $_
        Value = $Matches[1].trim()
      }
    }

我真的应该向下滚动,然后发布类似的答案。

答案 2 :(得分:0)

尝试一下:

编辑:

$FinalTable = Get-Content .\Contract.doc |
        select-string -pattern "in relation to any Facility" |
        Select -Property @{Name = 'Name'; Expression = {$_.Line}} |
        ForEach-Object {
        $str = $_.Name
        $start = $str.indexOf("[") + 1
        $end = $str.indexOf("]", $start)
        $length = $end - $start

        $result = ($str.substring($start, $length)).trim()

        #Creating a custom object to display in table format
        $Obj = New-Object -TypeName PSCustomObject
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Name -Value $str
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Value -Value $result

        $obj
    }
    $FinalTable