Script to extract lines from a large text file

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Yadav Manoj Yadav Manoj 1 year, 11 months ago.

  • Author
    Posts
  • #26557
    Profile photo of Yadav Manoj
    Yadav Manoj
    Participant

    Hi,

    I am trying to extract some lines from a huge text file using a powershell script. Wondering if anyone can help me in doing that.

    I have a huge file with all the system events and trying to extract all those lines/events which have the ip "172.16.2.62" in them.

    This is what I have till now:

    $log = get-content D:\scripts\iis.log
    foreach ($line in $log){
    $line
    }

    Please help.

    Thank you.

  • #26559
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Try Select-String -Path D:\scripts\iis.log -Pattern '172.16.2.62' -SimpleMatch

    Above cmdlet will return objects to the pipeline matching the lines and other details (line number, etc.)

  • #26561
    Profile photo of Yadav Manoj
    Yadav Manoj
    Participant

    Thank you very much. I tried it but I got this error:

    The string starting:
    At D:\scripts\Script.ps1:1 char:60
    + Select-String -Path D:\Scripts\iis.log -Pattern"172.16.2.62 < is missing the terminator: ". At D:\scripts\Script.ps1:1 char:73 + Select-String -Path D:\Scripts\iis.log -Pattern"172.16.2.62"- + CategoryInfo : ParserError: (-SimpleMatch:String + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

You must be logged in to reply to this topic.