How to print lines from nth line to nth line between two strings using powershell

Viewed 116

I am struggling to print lines from nth line to nth line between two strings using powershell. From the below sample data I want to print lines between '--------' to 'Jammer' except first line after '---------' to output.txt

This is my sample data 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                                                                                  
--------

Expected output: 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

This is what I have tried so far and I was able to get number of lines between strings but not able to print the desired output. Can someone please suggest how can I achieve this. Thanks in advance.

$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++;
}
1 Answers

You can try using regex but a simpler solution would be to use a simple state machine while processing each line to determine if

  • you have just read a '-------' and need to skip the next line
  • and if the line starts with C:\test, you should output the line

Example

@'
--------                                                                  
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 {$_}}
    }
}

Applied to your file

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