read lines from a log-file only for a given date

תשובה read lines from a log-file only for a given date

  • Tuesday, May 08, 2012 11:14 PM
     
     

    Hi,

    I have a very large log-file containing thousands of line like this:

    20110325 00:01:04     204.44.153.8  128.91.201.34     GET /focus/test19.mpg HTTP/1.0
    20110325 00:01:36     204.44.153.8  179.2.134.6         GET /focus/test2.html HTTP/1.0
    20110326 00:01:45     204.44.153.8  179.2.134.6         GET /focus/test10.gif HTTP/1.0
    20110326 00:02:32     204.44.153.8  179.2.134.6         GET /focus/test44.html HTTP/1.0
    20110326 00:02:42     204.44.153.8  179.2.134.6         GET /focus/test35.gif HTTP/1.0
    20110327 00:02:44     204.44.153.8  179.2.134.6         GET /focus/test20.gif HTTP/1.0

    If i want to collect only the lines that have date 20110326, instead of reading thousands lines (line-by-line) in this BIG log-file, what could be the best way to do it? Anyone please help?


All Replies

  • Tuesday, May 08, 2012 11:48 PM
     
     Answered Has Code

    One way to do it. Just change online.log to whatever the log file name is.

    switch -regex -file .\online.log { 
        "20110326" {
            ("{0}" -f $_)
        }
    }


    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

    • Marked As Answer by ThePathFinder Wednesday, May 09, 2012 8:45 AM
    •  
  • Wednesday, May 09, 2012 4:02 AM
     
     Proposed Answer Has Code

    Try this

    get-content C:\dummy.log | select-string "^20110326"

    you can easily pipe the result to a file with

    get-content C:\dummy.log | select-string "^20110326" | out-file c:\result.log


    Cyreli

  • Wednesday, May 09, 2012 4:05 AM
     
      Has Code

    Another method:

    Select-String -Path online.log -Pattern "20110326" | select line


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


  • Wednesday, May 09, 2012 1:42 PM
     
     

    Hi, I want to use the result for further proessing, so i believe the right way to get the result is assigning to a variable:

    $w = switch -regex -file .\online.log {
         "20110326" {
                  ("{0}" -f $_)
          }
         }

    BUT, its automatically making $w an array. Is there any way to get the output as a complete string, not an array?

  • Wednesday, May 09, 2012 1:44 PM
     
     
    $w = [string] $w


    • Edited by King Julien Wednesday, May 09, 2012 1:46 PM
    • Edited by King Julien Wednesday, May 09, 2012 1:46 PM
    • Edited by King Julien Wednesday, May 09, 2012 1:49 PM
    •  
  • Wednesday, May 09, 2012 1:45 PM
     
     Answered

    $w = $w | out-string


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked As Answer by ThePathFinder Wednesday, May 09, 2012 1:58 PM
    •  
  • Wednesday, May 09, 2012 2:02 PM
     
     
    $w = [string] $w



    This will indeed produce a string, but it will not insert CR/LF's and other non-printing characters.  Piping an array to Out-String keeps the formatting intact, just as a here-string would present on the screen.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


  • Wednesday, May 09, 2012 7:12 PM
     
     

    I just dont get the point of converting it to a string if he wants to keep its previous format. Whats the purpose behind this?

    • Edited by King Julien Wednesday, May 09, 2012 7:14 PM
    •  
  • Wednesday, May 09, 2012 7:16 PM
     
     
    I don't know why.  There could be many reasons.  Maybe he wants to use it as the body of an email.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)