PowerShell: find line but include proceeding and preceding line(s)

9,878

The Select-String cmdlet with -Context is pretty much a perfect match for what you're trying to do... This will read the contents of a file and search for a string, returning the 3 lines above and below the string:

$file = C:\Users\Public\Documents\log.txt
$string = "*error*"
Get-Content $file | Select-String $string -Context 3 -SimpleMatch

By default Select-String uses regular expressions, but -SimpleMatch will do a plain text search.

Share:
9,878

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    This is a conceptual question that I have only briefly thought through my head. I have idea how I might do it but it seems more complicated than I think I will make it out to be.

    A common need is for searching through a file (e.g. may be an error log) and I want to search that file for one particular string value. However in my case I thought some applications output an error on one line but the line that includes the error text could be on the proceeding or preceding line of that log (e.g. SQL Server error logs). So in this case I might want to include the line found with the specific string provided in the search, but include the line (or even lines) before and after it.

    I try to keep my scripts as simple as I can for readability of others that may use it or find it. I cannot think in my head this would be to easily written.

    EDIT

    Based on the answer provided using Select-String this is an example of what I am attempting to do:

    [void][System.Reflection.Assembly]::[LoadWithPartialName]('Microsoft.SqlServer.SMO')
    $s = New-Object Microsoft.SqlServer.Management.Smo.Server MyServer
    $s.EnumErrorLogs() | select -ExpandProperty Name | 
    foreach { $s.ReadErrorLog($_) | select-string -Pattern "*Serverity: 25*" -Context 3 -SimpleMatch | 
    select LogDate, ProcessInfo, Text
    
    • Admin
      Admin over 9 years
      Rereading my own question, it I a bit weak...I'd even down vote it myself.
    • Tim Ferrill
      Tim Ferrill over 9 years
      It might help if you spelled "Severity" correctly... Just sayin. :)
  • Admin
    Admin over 9 years
    I actually can't get this to work dealing with reading SQL Server error logs. I am reading them via SMO so don't know if that effects it, wouldn't think it would.
  • Tim Ferrill
    Tim Ferrill over 9 years
    Select-String is looking for a contiguous string, not object attributes. You might have to convert each line to a string and then back to columns in order to achieve the desired result. You might also post some of the output you're getting from SMO...
  • Admin
    Admin over 9 years
    That sounds to be more trouble than it is worth as I am trying to just keep this function as simple as possible. As I expect you are correct on the object property I think I will just pass on trying to add this functionality.
  • Jorge Luque
    Jorge Luque about 3 years
    Is there a way to only show preceding lines but not the lines after?