提取并排序大型文本文件

时间:2019-09-09 23:50:39

标签: powershell

我写了一些代码行,它们提取了一个大文本文件的信息。但是,它需要几天才能完成。有没有更好更快的方法?

这些是sna.txt中的某些行。有数千行,文件约为100MB。

WORKSTATION GGGGGG /ADD                         \

  /COMMENT:"GGG111"                             \

  /WKSTAONLY:Yes                                  \

  /ADDRESS:No                                     \

  /IPSUBNET:No                                    \

WORKSTATION GGGGGG                              \

  /INSERT:GGG222                                \

WORKSTATION GGGGGG                              \

  /INSERT:GGG333                                \

WORKSTATION GGGGGG                              \

  /INSERT:GGG444                                \

WORKSTATION GGGGGG                              \

  /INSERT:GGG555                               \

WORKSTATION HHHHHH /ADD                         \

  /COMMENT:"HHH111"                             \

  /WKSTAONLY:Yes                                  \

  /ADDRESS:No                                     \

  /IPSUBNET:No                                    \

WORKSTATION HHHHHH                              \

  /INSERT:HHH222                                \

WORKSTATION HHHHHH                              \

  /INSERT:HHH333        

这是我的代码

$SNAContent = $SNADevices = $snacomputerlist = $snacomputers = $null
$snasorted = '\\xxx\yyy\snasorted.txt'
$snacomputers = '\\xxx\yyy\snacomputers.txt'
$SNAPath = '\\xxx\yyy\sna.txt'
$SNAContent = Get-Content $SNACFGPath
$SNAContent | Select-String -Pattern '(WORKSTATION.*/ADD)' | ForEach-Object {
    $_ -replace "WORKSTATION" -replace "/ADD" -replace "\\" -replace " "
} | Out-File $snacfgcomputers -Append

Get-Content $snacfgcomputers | Where-Object {
    ($_ -notmatch 'aaaaaaaa') -or
    ($_ -notmatch 'bbbbbbbb')
} | Set-Content $snacfgcomputers

$snacomputerlist = Get-Content $snacomputers

$snacomputerlist | ForEach-Object {
    (($SNAContent | Select-String -Pattern $_ -Context 0,1 | ForEach-Object {
        $_.Context.PostContext[0].Trim() -replace "/COMMENT:" -replace "/INSERT:" -replace "\\"
    }) -join "," -replace " ","").Insert(0,"$_,") | Out-File $snacfgsorted -Append
}

这是snasorted.txt中的结果

GGGGGG,"GGG111",GGG222,GGG333,GGG444,GGG555
HHHHHH,"HHH111",HHH222,HHH333

2 个答案:

答案 0 :(得分:0)

也许这对您有帮助:

$sourceFilePath  = 'C:\test.txt'
$destFilePath    = 'C:\test1.txt'

$dt = New-Object system.Data.DataTable
[void]$dt.Columns.Add('WORKSTATION',[string]::empty.GetType() )
[void]$dt.Columns.Add('INSERT',[string]::empty.GetType() )

foreach($line in [System.IO.File]::ReadLines($sourceFilePath))
{
    if( $line ) {
        if( $line -like '*WORKSTATION*' ) {
            $workstation = @( $line -split ' ' | ? { $_ } )[1]
            $rows = $dt.Select("WORKSTATION = '$workstation'")

            if( $rows.Count -lt 1 ) {
                $newRow = $dt.NewRow()
                $newRow.WORKSTATION = $workstation
                $newRow.INSERT      = ''
                [void]$dt.Rows.Add( $newRow )
            }

        }
        elseif( $line -like '*INSERT*' ) {
            $insert = (@( $line -split ':' | ? { $_ } )[1]).Trim(' \')
            $rows   = $dt.Select("WORKSTATION = '$workstation'")

            if( $rows.Count -gt 0 ) {
                if( !(($rows[0]).INSERT).Contains( $insert ) ) {
                    ($rows[0]).INSERT += ',' + $insert
                    [void]$dt.AcceptChanges()
                }
            }
        }
    }
}

$dt.Select("", "WORKSTATION") | ConvertTo-Csv -NoTypeInformation | Select -Skip 1 | % { $_.Replace('"','').Replace(',,',',').Trim(',') } | Out-File $destFilePath -Force 

答案 1 :(得分:0)

我知道它效率低下,我将对其进行改进以使其更美观,更有效。

但是,从文件外内容更改为添加内容有很大不同。现在只需几分钟。