-2

(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

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 3
    Please show your's code progress. – Vad Mar 04 '20 at 06:58
  • m a beginner , looking for solution. – subyyal Khan Mar 04 '20 at 07:41
  • 1
    first you must to try do this and then show your code and then ask for help. be good if you show examles of your string,how you read it,and data from you files – Vad Mar 04 '20 at 07:46
  • @Vad i was using this command Get-Content input.log | Select-String -Pattern "ERROR" -Context 2,4 but it just outputs ERROR string along with subsequent lines. I want logically that if next line to this 'ERROR' have "Exception" then output Error+next 3 lines. like this.========================= ERROR (ProductStructuresGenerator.java:69) report:rowMode com.enterprisedt.net.ftp.FTPException: ======= – subyyal Khan Mar 04 '20 at 07:54

2 Answers2

1

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
#>
postanote
  • 15,138
  • 2
  • 14
  • 25
  • No worries. There are a bunch of very helpful folks here, but they really don't like doing other folks work for them from scratch. You'll regularly get statements like, SO is not a free code/script writing service, then they will send to the 'How to ask a question topic'. However, every once in a while, some my jump oin something, if it interest them. – postanote Mar 04 '20 at 08:19
0

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)]
  }
}
Wasif
  • 14,755
  • 3
  • 14
  • 34