Windows Vista Forums

Reading text files using powershell
  1. #1


    marzy Guest

    Reading text files using powershell

    Hi all.

    I have a particular problem.

    I have some log files that I want to search. I would like to use powershell
    to read the files and find a particular word.
    e.g. search for the word error in the log and tell me how many times it
    finds the word and report back.

    any help much appreciated.




      My System SpecsSystem Spec

  2. #2


    Keith Hill [MVP] Guest

    Re: Reading text files using powershell

    "marzy" <marzy@xxxxxx> wrote in message
    news:A203B368-A62A-4DEB-A08D-F8F1A2130321@xxxxxx

    > Hi all.
    >
    > I have a particular problem.
    >
    > I have some log files that I want to search. I would like to use
    > powershell
    > to read the files and find a particular word.
    > e.g. search for the word error in the log and tell me how many times it
    > finds the word and report back.
    >
    > any help much appreciated.
    >
    $regex = [regex]'(?i)\berror\b'
    Get-ChildItem *.log -r | Get-Content | Foreach { $regex.Matches($_) } |
    measure-object

    --
    Keith


      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: Reading text files using powershell

    If you want a count per log file report here are a couple of ways of doing it in v1.0 and v2.0 CTP

    $word = 'Error'
    # v1.0
    Get-ChildItem *.log |
    Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
    Select-Object FullName, @{name = 'Count';
    expression = {@((Get-Content $_.fullname |
    Out-String).split() -match "\b$word\b").count}} |
    Format-Table -auto

    # faster
    Get-ChildItem *.log |
    Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
    Select-Object FullName, @{name = 'Count';
    expression = {@([regex]::matches((Get-Content $_.fullname),
    "\b$word\b", 1)).count}} |
    Format-Table -auto

    # one-liner versions
    ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@((gc $_.fullname|out-string).split()-match"\b$word\b").count}}|ft -a
    # faster
    ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@([regex]::matches((gc $_.fullname),"\b$word\b",1)).count}}|ft -a

    ### v2.0 CTP ###
    Get-ChildItem *.log |
    Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
    Select-Object FullName, @{name = 'Count';
    expression = {@((gc $_.fullname |
    Out-String) -split ' ' -match "\b$word\b").count}} |
    Format-Table -auto
    # fastest
    Get-ChildItem *.log |
    Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
    Select-Object FullName, @{name = 'Count';
    expression = {Select-String "\b$word\b" $_.fullname -all |
    ForEach-Object {$total = 0} {$total += $_.matches.count} {$total}}} |
    Format-Table -auto

    # one-liner versions
    ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@((gc $_.fullname|Out-String)-split' '-match"\b$word\b").count}}|ft -a
    # fastest
    ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={Select-String "\b$word\b" $_.fullname -all|%{$total=0}{$total+=$_.matches.count}{$total}}}|ft -a

    --
    Kiron

      My System SpecsSystem Spec

Reading text files using powershell problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading A Text File vqthomf VB Script 2 19 Aug 2009
Searching for content in text files with powershell snofire PowerShell 5 31 Dec 2007
Text reading software.. ellisfaith Vista General 6 13 Nov 2007
Reading event log files - PowerShell and LogParser 2.2 Rob Campbell PowerShell 0 11 Apr 2007
Reading the BOM of text files Maximilian Hänel PowerShell 5 16 Oct 2006