如何修改此 Powershell 脚本?

问题描述

我有一个包含 60K+ 行的文本文件。那些 60K+ 行实际上是用 Natural 编写的大约 50 个左右的程序。 我需要将它们分解成单独的程序。我有一个脚本,可以完美地处理一个缺陷。输出文件的命名。

每个程序都以“模块名称=”开头,然后是程序的实际名称。我需要拆分程序并使用实际程序名称保存它们。

使用下面的示例,我想创建两个名为 Program1.txt 和 Program2.txt 的文件,每个文件都包含属于它们的行。我有一个脚本,也在下面,可以正确分隔文件,但我无法辨别捕获程序名称并将其用作输出文件名称的正确方法

示例:

Module Name=Program1
....
....
....
END

Module Name=Program2
....
....
....
END

代码

$InputFile = "C:\Natural.txt"
$Reader = New-Object System.IO.StreamReader($InputFile)
$a = 1
While (($Line = $Reader.ReadLine()) -ne $null) {
    If ($Line -match "Module Name=") {
        $OutputFile = "MySplittedFileNumber$a.txt"
        $a++
    }    
    Add-Content $OutputFile $Line
}

解决方法

结合一个switch statement,它可以高效地逐行读取文件,并且可以用-File将每一行与regex(es)匹配起来,并使用{{3}有效写入输出文件的实例:

-Regex

注意:

  • 代码假定输入文件的第一行是 $outStream = $null switch -Regex -File C:\Natural.txt { '\bModule Name=(\w+)' { # a module start line if ($outStream) { $outStream.Close() } $programName = $Matches[1] # Extract the program name. # Create a new output file. # Important: use a *full* path. $outStream = [System.IO.StreamWriter] "C:\$programName.txt" # Write the line at hand. $outStream.WriteLine($_) } default { # all other lines # Write the line at hand to the current output file. $outStream.WriteLine($_) } } if ($outStream) { $outStream.Close() } 行。

  • 默认情况下,正则表达式匹配不区分大小写,就像 PowerShell 通常一样;如果需要,添加 Module Name=...

  • System.IO.StreamWriter 用于从匹配结果中提取程序名称。

,

谢谢杰夫!

这是我使用拆分命令的解决方案

$InputFile = "C:\Temp\EMNCP\Natural.txt"
$Reader = New-Object System.IO.StreamReader($InputFile)

$OPName = @()
While (($Line = $Reader.ReadLine()) -ne $null) {
    If ($Line -match "Module Name=") {
        $OPName = $Line.Split("=")
        $FileName = $OPName[1].Trim()
        Write-Host "Found ... $FileName" -foregroundcolor green
        $OutputFile = "$FileName.txt"

    }    
    Add-Content $OutputFile $Line
}