subyyal Khan
subyyal Khan

Reputation: 1

PowerShell if next line contains "this string" ,output current line and next 3 lines else output just current line

(sorry for bad english)I need a PowerShell Script that does = if next line contains "this string" ,output current line and next 3 lines else output just current line. e.g

Error Info Error Error exeption Error

if next line do not have 'exeption' string ,output an excel file only error, but if next line to Error string contains exeption , return Error line + next 3 lines

Upvotes: -2

Views: 2086

Answers (2)

wasif
wasif

Reputation: 15478

Try like this:

# get file content as an array
$lines = Get-Content "filepath"
$line = 0
# read lines one by one
$lines | foreach {
  # increment line counter
  $line++
  if ($_ -match ".*(This string).*") {
    # Line matched show current line and next 3 lines from $lines array
    $_
    $lines[$line..($line+3)]
  }
}

Upvotes: 0

postanote
postanote

Reputation: 16096

What have you searched for?

The are many samples of this use case all over the web and right here on SO. Folks here are happy to help, but you must show the effort, the code, and the errors.

I normally don't do this for these types of posts, but let me get you started. So, the next time you visit, you are more prepared to help folks, help you.

Just search for ...

'PowerShell select string and next 3 lines'

... and always, read the help files, don't guess or assume, in this case, the Get-Content or Select-String cmdlets

# Help topics 
Get-Help about_*

# get function / cmdlet details
Get-help -Name Get-Content -Full
Get-help -Name Select-String -Full

... and review the -Context switch info and the examples. As well as the help files on conditional logic, i.e, if/then.

PowerShell: Select line preceding a match — Select-String -Context issue when using input string variable

Reading text file with -context

read the discussion and see the answer given.

So, your question can almost be considered a duplicate of the SO thread above, though it is looking in the reverse, which means you just need to switch the direction, as the 2nd example.

Update

OK, since you just posted some code ( though always update you question with the code vs putting it in the comment section so that it is easier to follow and for folks to get their heads around), showing you are going down the right track, it's really as simple something like this:

# Example
@'
line1
line2
line3
line4
line5
'@ | Out-File -FilePath 'D:\temp\FileWithLines.txt'

Get-Content -Path 'D:\temp\FileWithLines.txt' | 
Select-String -Pattern line2 -Context 0,3

<#
# Results

> line2
  line3
  line4
  line5
#>

Get-Content -Path 'D:\temp\FileWithLines.txt' | 
Select-String -Pattern line1 -Context 0,3
<#
# Results

> line1
  line2
  line3
  line4
#>

If (Get-Content -Path 'D:\temp\FileWithLines.txt' | Select-String -Pattern line0)
{
    Get-Content -Path 'D:\temp\FileWithLines.txt' | 
    Select-String -Pattern line1 -Context 0,3
}
Else 
{
    Write-Warning -Message "Current line is 
    $(Get-Content -Path 'D:\temp\FileWithLines.txt' | 
    Select-String -Pattern line1)"
}
<#
# Results

WARNING: Current line is 
    line1
#>

Upvotes: 1

Related Questions