Powershell 解析对象中的CSV文件

Powershell 解析对象中的CSV文件,powershell,parsing,Powershell,Parsing,我们有一些相当大的日志文件(3-8Gb),用空格分隔,有64个头。我需要搜索这些内容并拉出搜索词,但只需要64个头中的5个头中的数据 我遇到了托比亚斯·韦尔特纳。看了之后,我有了一些代码片段,但似乎陷入了实际得到任何结果的困境 基本上,我需要从一个更大的文件中搜索5个标题。到目前为止,我掌握的代码是: $Search = "J89HD" $logfile = "C:\Logs\CP8945KGT.log" ForEach-Object { $line = $_ $infos

我们有一些相当大的日志文件(3-8Gb),用空格分隔,有64个头。我需要搜索这些内容并拉出搜索词,但只需要64个头中的5个头中的数据

我遇到了托比亚斯·韦尔特纳。看了之后,我有了一些代码片段,但似乎陷入了实际得到任何结果的困境

基本上,我需要从一个更大的文件中搜索5个标题。到目前为止,我掌握的代码是:

$Search = "J89HD"
$logfile = "C:\Logs\CP8945KGT.log"

ForEach-Object {

    $line = $_
    $infos = $line -split " "

    $hashtable = [Ordered]@{}
        $hashtable.date = $infos[0] 
        $hashtable.time = $infos[1]
        $hashtable.Index = $infos[2]
        $hashtable.source = $infos[3]
        $hashtable.destination = $infos[-1]

    New-Object -TypeName psobject -Property $hashtable

    Get-Content -Path $hashtable |
        Where-Object { $_ -match "$Search" } |
        Select-Object -Last 20 |
        Out-GridView
}
我得到的错误消息是:

Get-Content: Cannot find path 'C:\System.Collections.Specialized.OrderedDictionary' because it does not exist.
At C:\scripts\testing01.ps1:17 char:1
+ Get-Content -Path  $hashtable | Where-Object {$_ -match "$Search"} |  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\System.Colle...deredDictionary:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

下面是一个基于您想要做的事情和代码片段的代码片段。这是未经测试的,因为我们没有要测试的样本输入数据

$Search = "J89HD"
$logfile = "C:\Logs\CP8945KGT.log"

#load file
Get-Content -Path $logfile |

    #apply search filter on each line
    Where-Object { $_ -match $Search } |

    #keep only last 20 lines
    Select-Object -Last 20 |

    #for each line
    ForEach-Object {

        #store the line in a variable
        $line = $_

        #split the line on spaces to get an array
        $infos = $line -split " "

        #build a hashtable with properties holding specific cells of the array
        $hashtable = [Ordered]@{
            date = $infos[0] 
            time = $infos[1]
            Index = $infos[2]
            source = $infos[3]
            destination = $infos[-1]
        }

        #build a custom object with the properties in the hastable
        New-Object -TypeName psobject -Property $hashtable

    #display the objects in a window
    } | Out-GridView
我将尝试解释您的语法有什么问题:

  • ForEach对象
    块是为管道中的每个元素处理的,因此必须将其放入管道中

  • hashtable
    声明的结束
    }
    应该在属性之后

  • Get Content-Path
    需要一个文件路径,而您正在给它一个
    哈希表
    (您得到的错误就是由于这个原因)

  • Get Content
    不应位于
    ForEach对象
    块中,因为您不希望多次加载文件;这是你管道的开始


  • 您没有向
    ForEach对象提供对象,因为它可以迭代。日志文件有头吗?在编辑的问题中显示日志示例。非常感谢编辑的脚本,但它会抛出错误消息。哈希文本中不允许使用空键。在C:\scripts\Log-Parser2.ps1:24 char:13+$hashtable.date=$infos[0]+~~~~~~~~~~~~~~~~~~~~+CategoryInfo:InvalidOperation:(System.Collecti…deredDictionary:OrderedDictionary)[],RuntimeException+FullyQualifiedErrorId:InvalidNullKeyI还能够获得示例数据的精简版本。虽然真正的日志文件非常大,并且有更多的数据,但这是我在查询它时想要提取的关键信息。“C:\Logs\CP8945KGT.log”2017.05.19:16 C J2-Send-CL6TT0101012017 KL878EJDH--88PL34-GOOLE021223 KL878EJDH--6HDJW 2017.05.19:16 A J2-Send-CL6TT0101022017 KL878EJDH--88PL34-GOOLE022017 KL878EJDH--KDKJF 2017.05.19:16 B J2-Send-CL6TT0101032017 KL878EJDH--88PL34-GOOL01032017 KL878EJDH--DJWJS 2017.05.19:16 A J2-Send-CL6TT0101042017 KL878EJDH--88PL34-GOOLE014217 KL878EJDH--8327E请添加此功能。它按预期工作,非常感谢您的帮助。你犯了什么愚蠢的错误。。有没有好的链接来确定其他delimeter的代码应该是什么,比如tab、逗号或分号。