【问题标题】:How to print lines from nth line to nth line between two strings using powershell如何使用powershell在两个字符串之间从第n行打印到第n行
【发布时间】:2020-11-27 05:15:08
【问题描述】:

我正在努力使用 powershell 在两个字符串之间打印从第 n 行到第 n 行的行。 从下面的示例数据中,我想打印 '--------' 到 'Jammer' 之间的行,除了 '---------' 之后的第一行到 output.txt

这是我的示例数据 sample.txt

--------                                                                  
C:\test\1528372320775.xml                      
C:\test\fail\1528372320775_877291.xml            
C:\test\outgoing\1528372320775_xexAx2111.xml
   Jammer: 2C339A2A:138814                                                                                         
--------                                                                     
C:\test\244124414.xml                      
C:\test\fail\244124414_232423.xml            
C:\test\outgoing\244124414_xexAx21333.xml
   Jammer: 8373SDF4:138814                                                                                  
--------
C:\test\832492892387.xml                      
C:\test\fail\832492892387_2SU2932.xml             
C:\test\outgoing\832492892387_xexAx23232.xml
   Jammer: 3G380DD2A:338783                                                                                  
--------
C:\test\23234923427.xml                      
C:\test\fail\23234923427_2SLLE.xml               
C:\test\outgoing\23234923427_xexAx2233.xml
C:\test\exeption\23234923427_xexAx2233.xml
   Jammer: 3883BB3SC:311314                                                                                  
--------

预期输出:output.txt

C:\test\fail\1528372320775_877291.xml            
C:\test\outgoing\1528372320775_xexAx2111.xml
C:\test\fail\244124414_232423.xml            
C:\test\outgoing\244124414_xexAx21333.xml
C:\test\fail\832492892387_2SU2932.xml             
C:\test\outgoing\832492892387_xexAx23232.xml
C:\test\fail\23234923427_2SLLE.xml               
C:\test\outgoing\23234923427_xexAx2233.xml
C:\test\exeption\23234923427_xexAx2233.xml

这是我到目前为止所尝试的,我能够获得字符串之间的行数,但无法打印所需的输出。有人可以建议我如何实现这一目标。提前致谢。

$file = "C:\Sample.txt"
$start="--------"
$end="Jammer"

$lines = (Select-String -Path $file -Pattern $start).LineNumber
$endlines = (Select-String -Path $file -Pattern $end).LineNumber

#Write-Host $endlines
#$lines.Count
#$endlines.Count

$i=0;
foreach($endline in $endlines) 
{
   $numberoflines = ($endline - $lines[$i] -1)
   $linetoprint = $lines[$i]+1
   
  for($j=1; $j-eq$j+$numberoflines; $j++) 
  {
     Get-Content $file | Select -Index $linetoprint
  }
  Write-Host $numberoflines
  $i++;
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以尝试使用正则表达式,但更简单的解决方案是使用简单的状态机,同时处理每一行以确定是否

    • 您刚刚阅读了“------”,需要跳过下一行
    • 如果该行以C:\test 开头,则应输出该行

    示例

    @'
    --------                                                                  
    C:\test\1528372320775.xml                      
    C:\test\fail\1528372320775_877291.xml            
    C:\test\outgoing\1528372320775_xexAx2111.xml
       Jammer: 2C339A2A:138814                                                                                         
    --------                                                                     
    C:\test\244124414.xml                      
    C:\test\fail\244124414_232423.xml            
    C:\test\outgoing\244124414_xexAx21333.xml
       Jammer: 8373SDF4:138814                                                                                  
    --------
    C:\test\832492892387.xml                      
    C:\test\fail\832492892387_2SU2932.xml             
    C:\test\outgoing\832492892387_xexAx23232.xml
       Jammer: 3G380DD2A:338783                                                                                  
    --------
    C:\test\23234923427.xml                      
    C:\test\fail\23234923427_2SLLE.xml               
    C:\test\outgoing\23234923427_xexAx2233.xml
    C:\test\exeption\23234923427_xexAx2233.xml
       Jammer: 3883BB3SC:311314                                                                                  
    --------
    '@ -split "`r`n" | % {
        switch -Regex ($_) {
            '--------' {$skipnext=$true}
            '^C:\\test' {if ($skipnext) {$skipnext = $false} else {$_}}
        }
    }
    

    应用于您的文件

    switch -File (gc <yourfile>) -Regex {
        '--------' {$skipnext=$true}
        '^C:\\test' {if ($skipnext) {$skipnext = $false} else {$_}}
        }
    }
    

    【讨论】:

    • 如果你想从文件中获取它,只需将第一部分替换为-split 原样:(Get-Content .\myfile.txt) -split "``r``n"rn 之前只有一个反引号)(是有人可以告诉我如何在评论中的代码中获取反引号?,我在帮助中找不到它)
    • @CFou - 不知道 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2022-12-18
    • 2013-02-23
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多