简体   繁体   English

Powershell 匹配当前行和下一行然后输出文件

[英]Powershell to match the current line and next line then out-file

I'm trying to extract the data whereby:我正在尝试提取数据,其中:

line 1 = Report ID + Line 2 = "Machine no" + Line 3 = OFFLINE第 1 行 = 报告 ID + 第 2 行 =“机器编号”+ 第 3 行 = 离线

Then Out-File to a new file.然后Out-File到一个新文件。

Sample data样本数据

Report ID           page1

Machine no  1234    

        OTHERS
            12
        offline
            12
        OTHERS
            23
        offline
            37
        OTHERS
            89
        offline
            65

The result I'm looking for look something like the below after processing:处理后,我正在寻找的结果类似于以下内容:

Report ID           page 4

    Machine no  1234    
        offline
            12
        offline
            37
        offline
            65

You can use the Select-String cmdlet with the -Context Parameter to search through a file and then select how many lines of contextual info you want to get back from your search.您可以使用带有-Context参数的Select-String cmdlet 来搜索文件,然后选择要从搜索中获取多少行上下文信息。

For instance, if we take your input file and store it in a variable called $input like so:例如,如果我们将您的输入文件存储在一个名为$input的变量中,如下所示:

$inputFile= Get-Content C:\Path\To\YourFile.txt
$inputFile| Select-string 'machine no'
>Machine no  1234  

We can then find matches for the phrase 'offline' with this:然后,我们可以使用以下命令找到短语 'offline' 的匹配项:

$inputFile| Select-String offline -Context 0,1

This states that I want you to search for the word 'offline' and give me zero lines proceeding it, and one line after it, giving us this output:这表明我希望你搜索“离线”这个词,然后给我零行,然后再给我一行,给我们这个输出:

>         offline
              12
>         offline
              37
>         offline
              65

We can put this all together to build this and generate a new output file that would look like this.我们可以将所有这些放在一起来构建它并生成一个新的输出文件,看起来像这样。

$out= ''
$out += $inputFile| Select-string 'machine no'
$out += "`n"
$out += $inputFile| Select-String offline -Context 0,1 | ForEach-Object {$_.Context.DisplayPostContext}

#Resulting file would look this, just the name of the machine and then the number of each offline...uh, whatever it is.
    Machine no  1234    
        12             37             65

If I were you, I'd adapt this flow to make PowerShell objects and properties instead, like this:如果我是你,我会调整这个流程来创建 PowerShell 对象和属性,如下所示:

$Report = [pscustomobject]@{ID='';MachineNo='';OfflineMembers=@()}
$Report.ID = ($inputFile | select-string page -Context 0 ).ToString().Split('page')[-1]
$Report.MachineNo = ($inputFile | Select-string 'machine no').ToString().Trim().Split()[-1]
$Report.OfflineMembers = $inputFile | Select-String offline -Context 0,1 | ForEach-Object {
            [pscustomobject]@{Value='Offline';ID=$_.Context.DisplayPostContext.Trim()}
            }

>$Report

ID MachineNo OfflineMembers                                                             
-- --------- --------------                                                             
1 1234      {@{Value=Offline; ID=12}, @{Value=Offline; ID=37}, @{Value=Offline; ID=65}}
 $Report.OfflineMembers
Value   ID
-----   --
Offline 12
Offline 37
Offline 65

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM