字符串匹配到数据表

时间:2013-11-11 19:25:56

标签: powershell powershell-v2.0

我需要从包含特定键值的文本行中提取几条信息。看起来这是一个相当普遍的情况,但我找不到太多有用的信息。目前我使用select-string查找包含“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

1 个答案:

答案 0 :(得分: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
    }

将您的文件替换为我正在使用的字符串。