过滤数据部分,包括起始行和结束行 - PowerShell

时间:2016-10-25 11:56:23

标签: powershell powershell-v4.0

我有一个如下所示的文本文件:

Data I'm NOT looking for  
More data that doesn't matter  
Even more data that I don't

&Start/Finally the data I'm looking for  
&Data/More data that I need  
&Stop/I need this too  

&Start/Second batch of data I need  
&Data/I need this too 
&Stop/Okay now I'm done  
Ending that I don't need  

以下是输出所需的内容:

FILE1.TXT

&Start/Finally the data I'm looking for  
&Data/More data that I need   
&Stop/I need this too  

FILE2.TXT

&Start/Second batch of data I need  
&Data/I need this too 
&Stop/Okay now I'm done  

我需要为文件夹中的每个文件执行此操作(有时会有多个文件需要过滤。)文件名称可以递增:ex。 File1.txt,File2.txt,File3.txt。

这就是我没有运气的尝试:

ForEach-Object{
$text -join "`n" -split '(?ms)(?=^&START)' -match '^&START' | 
Out-File B:\PowerShell\$filename}

谢谢!

3 个答案:

答案 0 :(得分:1)

看起来你非常接近:你的代码正确地提取了感兴趣的段落,但是非& - 起始行的段内输出过滤丢失了,你需要写入段落特定的输出文件:

$text -join "`n" -split '(?m)(?=^&Start)' -match '^&Start' | 
  ForEach-Object { $ndx=0 } { $_ -split '\n' -match '^&' | Out-File "File$((++$ndx)).txt" }

这为每个感兴趣的段落创建以File1.txt开头的顺序编号文件。

为文件夹中的每个文件执行此操作,输出文件名在所有输入文件中使用固定命名方案File<n>(以及累积编号):

Get-ChildItem -File . | ForEach-Object -Begin { $ndx=0 } -Process {
  (Get-Content -Raw $_) -split '(?m)(?=^&Start)' -match '^&Start' | 
    ForEach-Object { $_ -split '\n' -match '^&' | Out-File "File$((++$ndx)).txt" }
}

为文件夹中的每个文件执行此操作,输出文件名基于输入文件名和每个输入文件的编号(PSv4 +,由于使用-PipelineVariable):

Get-ChildItem -File . -PipelineVariable File | ForEach-Object {
 (Get-Content -Raw $_) -split '(?m)(?=^&Start)' -match '^&Start' | 
  ForEach-Object {$ndx=0} { $_ -split '\n' -match '^&' | Out-File "$($File.Name)$((++$ndx)).txt" }
}

答案 1 :(得分:1)

你发布了第二个问题(违反规则),它已被删除,但这是我的快速答案。我希望它能帮助你,让你更了解PS的工作原理:

$InputFile = "C:\temp\test\New folder (3)\File1.txt"

# get file content
$a=Get-Content $InputFile

# loop for every line in range 2 to last but one
for ($i=1; $i -lt ($a.count-1); $i++)
    {
    #geting string part between & and / , and construct output file name
    $OutFile = "$(Split-Path $InputFile)\$(($a[$i] -split '/')[0] -replace '&','').txt"

    $a[0]| Out-File $OutFile #creating output file and write first line in it
    $a[$i]| Out-File $OutFile -Append #write info line
    $a[-1]| Out-File $OutFile -Append #write last line
    }

答案 2 :(得分:0)

这样的东西?

 $i=0
 gci -path "C:\temp\ExplodeDir" -file | %{ (get-content -path $_.FullName -Raw).Replace("`r`n`r`n", ";").Replace("`r`n", "~").Split(";") | %{if ($_ -like "*Start*") {$i++; ($_ -split "~") | out-file "C:\temp\ResultFile\File$i.txt" }} }