0

我需要从包含某个键值的一行文本中提取几条信息。看起来这将是一个相当普遍的情况,但我无法找到很多有用的信息。目前我正在使用选择字符串来查找包含“242200”或“242201”的所有行。一旦确定了这些行,我就会尝试提取部分行并将它们放入数据表中。然后我需要对 DT 的两列求和并将结果导出为 CSV。这是我到目前为止所拥有的:

# Create a datatable to store the values in
$dataTable = New-Object System.Data.DataTable            

$dataTable.Columns.Add("Line Number")  | Out-Null
$dataTable.Columns.Add("Transaction Date")  | Out-Null
$dataTable.Columns.Add("ID")   | Out-Null
$dataTable.Columns.Add("Premium Tax State") | Out-Null            
$dataTable.Columns.Add("Amt1", [double]) | Out-Null            
$dataTable.Columns.Add("Amt2", [double]) | Out-Null            

ForEach($match in Select-String -path $SourceFile -pattern "242200", "242201") {

    # Convert the match OBJECT to a string
    $string = $match.Line

    $row = $datatable.NewRow()
    $row["Line Number"] = $match.LineNumber
    $row["Transaction Date"] = $string.substring(3,8)
    $row["ID"] = "MA4650654" #Need to find this in the string
    $row["Premium Tax State"] = $string.substring(32,34)

    # Determine the type and amount
    if ($string -match "242200") {
        $row["Amt1"] = 0 # Need to extract these amounts and sum them
        $row["Amt2"]  = 0
    }
    else {
        $row["Amt1"] = 0 
        $row["Amt2"]  = 0
    }

    $dataTable.Rows.Add($row)
}

有没有更好的方法可以通过匹配组或其他方式来做到这一点?

编辑:样本输入

T0120131107004311         116212OK      0000000300000CYCLE 11/07/2013        JERAIEROKCAR0025593WTM
T0120131107004311         242200MO      0000000022782CYCLE 11/07/2013        MSDQONEAZCAR0012366RMM
T0120131107004311         242200NC      0000000040541CYCLE 11/07/2013        MSDQONENCCAR0033278WTM
T0120131107004311         242201FL      0000000084831CYCLE 11/07/2013        VIIEAMAFLCAR0025589RMM
4

1 回答 1

0

尝试这样的事情(更新为在 V2 上工作):

$str = @"
T0120131107004311         116212OK      0000000300000CYCLE 11/07/2013        JERAIEROKCAR0025593WTM
T0120131107004311         242200MO      0000000022782CYCLE 11/07/2013        MSDQONEAZCAR0012366RMM
T0120131107004311         242200NC      0000000040541CYCLE 11/07/2013        MSDQONENCCAR0033278WTM
T0120131107004311         242201FL      0000000084831CYCLE 11/07/2013        VIIEAMAFLCAR0025589RMM
"@
$str -split "`n" | 
    Select-String -Pattern '^.{3}(.{8}).*?(?:242200|242201)(\w{2}).*?(.{10}).{3}\s*$' |
    Foreach {
        $_.matches | Foreach {$_.groups[1].value} # Date
        $_.matches | Foreach {$_.groups[2].value} # State
        $_.matches | Foreach {$_.groups[3].value} # Term at end
    }

用你的文件替换我正在使用的字符串。

于 2013-11-11T20:27:24.683 回答